How can I reference a JavaScript object with a variable as identifier? [duplicate]












0
















This question already has an answer here:




  • Dynamic variable name in loop

    2 answers




I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:



var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};


This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them



var lokVan = lok001;
outputObject.innerHTML = lokVan.x;


outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".



How can I get this to work?



Edit: I solved it. Instead of using



var lokVan = lok001;


which reads lok001 as a string, I had to use this



var lokVan = eval(lok001);


This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string



Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do



van = 003;
lokalen[van].x;


where



var lokalen = {
'001': { floor: 0, x: 1, y: 1 },
'002': { floor: 0, x: 2, y: 2 },
'003': { floor: 0, x: 3, y: 3 },
...
}









share|improve this question















marked as duplicate by T.J. Crowder javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 8:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Dynamic variable name in loop

      2 answers




    I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:



    var lok001 = {floor: 0, x: 2037, y: 713};
    var lok002 = {floor: 0, x: 1759, y: 743};


    This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them



    var lokVan = lok001;
    outputObject.innerHTML = lokVan.x;


    outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".



    How can I get this to work?



    Edit: I solved it. Instead of using



    var lokVan = lok001;


    which reads lok001 as a string, I had to use this



    var lokVan = eval(lok001);


    This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string



    Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do



    van = 003;
    lokalen[van].x;


    where



    var lokalen = {
    '001': { floor: 0, x: 1, y: 1 },
    '002': { floor: 0, x: 2, y: 2 },
    '003': { floor: 0, x: 3, y: 3 },
    ...
    }









    share|improve this question















    marked as duplicate by T.J. Crowder javascript
    Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 26 '18 at 8:18


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Dynamic variable name in loop

        2 answers




      I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:



      var lok001 = {floor: 0, x: 2037, y: 713};
      var lok002 = {floor: 0, x: 1759, y: 743};


      This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them



      var lokVan = lok001;
      outputObject.innerHTML = lokVan.x;


      outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".



      How can I get this to work?



      Edit: I solved it. Instead of using



      var lokVan = lok001;


      which reads lok001 as a string, I had to use this



      var lokVan = eval(lok001);


      This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string



      Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do



      van = 003;
      lokalen[van].x;


      where



      var lokalen = {
      '001': { floor: 0, x: 1, y: 1 },
      '002': { floor: 0, x: 2, y: 2 },
      '003': { floor: 0, x: 3, y: 3 },
      ...
      }









      share|improve this question

















      This question already has an answer here:




      • Dynamic variable name in loop

        2 answers




      I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:



      var lok001 = {floor: 0, x: 2037, y: 713};
      var lok002 = {floor: 0, x: 1759, y: 743};


      This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them



      var lokVan = lok001;
      outputObject.innerHTML = lokVan.x;


      outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".



      How can I get this to work?



      Edit: I solved it. Instead of using



      var lokVan = lok001;


      which reads lok001 as a string, I had to use this



      var lokVan = eval(lok001);


      This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string



      Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do



      van = 003;
      lokalen[van].x;


      where



      var lokalen = {
      '001': { floor: 0, x: 1, y: 1 },
      '002': { floor: 0, x: 2, y: 2 },
      '003': { floor: 0, x: 3, y: 3 },
      ...
      }




      This question already has an answer here:




      • Dynamic variable name in loop

        2 answers








      javascript cordova javascript-objects






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 14:19







      Bjorn Pijnacker

















      asked Nov 26 '18 at 8:15









      Bjorn PijnackerBjorn Pijnacker

      11




      11




      marked as duplicate by T.J. Crowder javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 26 '18 at 8:18


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by T.J. Crowder javascript
      Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 26 '18 at 8:18


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          -1














          You should use = sign to assing objects to variables.



          var lok001 = {floor: 0, x: 2037, y: 713};
          var lok002 = {floor: 0, x: 1759, y: 743};





          share|improve this answer
























          • Just checked, I did do this in my code, I just forget to put it in here

            – Bjorn Pijnacker
            Nov 26 '18 at 8:45



















          -1














          Did you just miss the = between var lok001 and {...};?






          share|improve this answer






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            -1














            You should use = sign to assing objects to variables.



            var lok001 = {floor: 0, x: 2037, y: 713};
            var lok002 = {floor: 0, x: 1759, y: 743};





            share|improve this answer
























            • Just checked, I did do this in my code, I just forget to put it in here

              – Bjorn Pijnacker
              Nov 26 '18 at 8:45
















            -1














            You should use = sign to assing objects to variables.



            var lok001 = {floor: 0, x: 2037, y: 713};
            var lok002 = {floor: 0, x: 1759, y: 743};





            share|improve this answer
























            • Just checked, I did do this in my code, I just forget to put it in here

              – Bjorn Pijnacker
              Nov 26 '18 at 8:45














            -1












            -1








            -1







            You should use = sign to assing objects to variables.



            var lok001 = {floor: 0, x: 2037, y: 713};
            var lok002 = {floor: 0, x: 1759, y: 743};





            share|improve this answer













            You should use = sign to assing objects to variables.



            var lok001 = {floor: 0, x: 2037, y: 713};
            var lok002 = {floor: 0, x: 1759, y: 743};






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 26 '18 at 8:22









            ShizonShizon

            12




            12













            • Just checked, I did do this in my code, I just forget to put it in here

              – Bjorn Pijnacker
              Nov 26 '18 at 8:45



















            • Just checked, I did do this in my code, I just forget to put it in here

              – Bjorn Pijnacker
              Nov 26 '18 at 8:45

















            Just checked, I did do this in my code, I just forget to put it in here

            – Bjorn Pijnacker
            Nov 26 '18 at 8:45





            Just checked, I did do this in my code, I just forget to put it in here

            – Bjorn Pijnacker
            Nov 26 '18 at 8:45













            -1














            Did you just miss the = between var lok001 and {...};?






            share|improve this answer




























              -1














              Did you just miss the = between var lok001 and {...};?






              share|improve this answer


























                -1












                -1








                -1







                Did you just miss the = between var lok001 and {...};?






                share|improve this answer













                Did you just miss the = between var lok001 and {...};?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 8:22









                r0bnetr0bnet

                2414




                2414















                    Popular posts from this blog

                    Lallio

                    Futebolista

                    Jornalista