How to get name of current JSON property while iterating through them in JavaScript?










5














I have JSON object inside variable like this:



var chessPieces = 
"p-w-1" :
"role":"pawn",
"position":"x":1, "y":2,
"state":"free",
"virgin":"yes"
,
"p-w-2" :
"role":"pawn",
"position":"x":2, "y":2,
"state":"free",
"virgin":"yes"
,...
;


And I'm iterating trough them with for each loop:



for (var piece in chessPieces)
//some code



How would I get current pieces name from this? For example, we are currently on the first element(piece = 0): chessPiece[piece].GiveMeTheName ==> which results in string "p-w-1".



I actually intend to pass the current element into the function, cos I need to check something, so it would look something like this:



//constructor for this function looks like this: function setPiece(piece,x,y);
function setPiece(chessPiece[piece],chessPiece[piece].position.x,chessPiece[piece].position.y)
//and here I need to get something like
piece.GiveMeTheName ==> which gives me string "p-w-1"



I'm also using jQuery in my project, so if there's something usable in that library, let me know.










share|improve this question























  • for (var piece in ...) piece itself holds the piece name
    – mplungjan
    May 31 '11 at 18:18










  • Drat. I really lose rep when I comment before I answer :(
    – mplungjan
    May 31 '11 at 19:04















5














I have JSON object inside variable like this:



var chessPieces = 
"p-w-1" :
"role":"pawn",
"position":"x":1, "y":2,
"state":"free",
"virgin":"yes"
,
"p-w-2" :
"role":"pawn",
"position":"x":2, "y":2,
"state":"free",
"virgin":"yes"
,...
;


And I'm iterating trough them with for each loop:



for (var piece in chessPieces)
//some code



How would I get current pieces name from this? For example, we are currently on the first element(piece = 0): chessPiece[piece].GiveMeTheName ==> which results in string "p-w-1".



I actually intend to pass the current element into the function, cos I need to check something, so it would look something like this:



//constructor for this function looks like this: function setPiece(piece,x,y);
function setPiece(chessPiece[piece],chessPiece[piece].position.x,chessPiece[piece].position.y)
//and here I need to get something like
piece.GiveMeTheName ==> which gives me string "p-w-1"



I'm also using jQuery in my project, so if there's something usable in that library, let me know.










share|improve this question























  • for (var piece in ...) piece itself holds the piece name
    – mplungjan
    May 31 '11 at 18:18










  • Drat. I really lose rep when I comment before I answer :(
    – mplungjan
    May 31 '11 at 19:04













5












5








5


1





I have JSON object inside variable like this:



var chessPieces = 
"p-w-1" :
"role":"pawn",
"position":"x":1, "y":2,
"state":"free",
"virgin":"yes"
,
"p-w-2" :
"role":"pawn",
"position":"x":2, "y":2,
"state":"free",
"virgin":"yes"
,...
;


And I'm iterating trough them with for each loop:



for (var piece in chessPieces)
//some code



How would I get current pieces name from this? For example, we are currently on the first element(piece = 0): chessPiece[piece].GiveMeTheName ==> which results in string "p-w-1".



I actually intend to pass the current element into the function, cos I need to check something, so it would look something like this:



//constructor for this function looks like this: function setPiece(piece,x,y);
function setPiece(chessPiece[piece],chessPiece[piece].position.x,chessPiece[piece].position.y)
//and here I need to get something like
piece.GiveMeTheName ==> which gives me string "p-w-1"



I'm also using jQuery in my project, so if there's something usable in that library, let me know.










share|improve this question















I have JSON object inside variable like this:



var chessPieces = 
"p-w-1" :
"role":"pawn",
"position":"x":1, "y":2,
"state":"free",
"virgin":"yes"
,
"p-w-2" :
"role":"pawn",
"position":"x":2, "y":2,
"state":"free",
"virgin":"yes"
,...
;


And I'm iterating trough them with for each loop:



for (var piece in chessPieces)
//some code



How would I get current pieces name from this? For example, we are currently on the first element(piece = 0): chessPiece[piece].GiveMeTheName ==> which results in string "p-w-1".



I actually intend to pass the current element into the function, cos I need to check something, so it would look something like this:



//constructor for this function looks like this: function setPiece(piece,x,y);
function setPiece(chessPiece[piece],chessPiece[piece].position.x,chessPiece[piece].position.y)
//and here I need to get something like
piece.GiveMeTheName ==> which gives me string "p-w-1"



I'm also using jQuery in my project, so if there's something usable in that library, let me know.







javascript jquery json for-loop each






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 2:07









Cœur

17.5k9104145




17.5k9104145










asked May 31 '11 at 18:14









HappyHappy

2814




2814











  • for (var piece in ...) piece itself holds the piece name
    – mplungjan
    May 31 '11 at 18:18










  • Drat. I really lose rep when I comment before I answer :(
    – mplungjan
    May 31 '11 at 19:04
















  • for (var piece in ...) piece itself holds the piece name
    – mplungjan
    May 31 '11 at 18:18










  • Drat. I really lose rep when I comment before I answer :(
    – mplungjan
    May 31 '11 at 19:04















for (var piece in ...) piece itself holds the piece name
– mplungjan
May 31 '11 at 18:18




for (var piece in ...) piece itself holds the piece name
– mplungjan
May 31 '11 at 18:18












Drat. I really lose rep when I comment before I answer :(
– mplungjan
May 31 '11 at 19:04




Drat. I really lose rep when I comment before I answer :(
– mplungjan
May 31 '11 at 19:04












3 Answers
3






active

oldest

votes


















2














Erm. Isn't piece already the name of the object? The for ... in in JavaScript gives you the key name.



So when you do for (var piece in chessPieces) console.log(piece);, it will print out p-w-1, p-w-2 etc






share|improve this answer




















  • Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
    – Happy
    May 31 '11 at 18:24







  • 1




    It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
    – Pwnna
    May 31 '11 at 18:25


















7














I'd use $.each(obj, fn). The function allows access to the object key of the current element.



$.each(chessPieces, function(key, value) 

//key = "p-w-1"
//value = "role":"pawn", ...
//this === value

);





share|improve this answer




























    4














    for (var piece in chessPieces)
    alert(piece)






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6191707%2fhow-to-get-name-of-current-json-property-while-iterating-through-them-in-javascr%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Erm. Isn't piece already the name of the object? The for ... in in JavaScript gives you the key name.



      So when you do for (var piece in chessPieces) console.log(piece);, it will print out p-w-1, p-w-2 etc






      share|improve this answer




















      • Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
        – Happy
        May 31 '11 at 18:24







      • 1




        It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
        – Pwnna
        May 31 '11 at 18:25















      2














      Erm. Isn't piece already the name of the object? The for ... in in JavaScript gives you the key name.



      So when you do for (var piece in chessPieces) console.log(piece);, it will print out p-w-1, p-w-2 etc






      share|improve this answer




















      • Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
        – Happy
        May 31 '11 at 18:24







      • 1




        It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
        – Pwnna
        May 31 '11 at 18:25













      2












      2








      2






      Erm. Isn't piece already the name of the object? The for ... in in JavaScript gives you the key name.



      So when you do for (var piece in chessPieces) console.log(piece);, it will print out p-w-1, p-w-2 etc






      share|improve this answer












      Erm. Isn't piece already the name of the object? The for ... in in JavaScript gives you the key name.



      So when you do for (var piece in chessPieces) console.log(piece);, it will print out p-w-1, p-w-2 etc







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered May 31 '11 at 18:18









      PwnnaPwnna

      4,104155282




      4,104155282











      • Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
        – Happy
        May 31 '11 at 18:24







      • 1




        It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
        – Pwnna
        May 31 '11 at 18:25
















      • Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
        – Happy
        May 31 '11 at 18:24







      • 1




        It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
        – Pwnna
        May 31 '11 at 18:25















      Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
      – Happy
      May 31 '11 at 18:24





      Thanks, I was confused since chrome console gave me undefined when typing in chessPieces[1]
      – Happy
      May 31 '11 at 18:24





      1




      1




      It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
      – Pwnna
      May 31 '11 at 18:25




      It's chessPieces, and it's suppose to be a hash table, which is also an object in JavaScript. Having the index of 1 seems.. un-js-objecty.
      – Pwnna
      May 31 '11 at 18:25













      7














      I'd use $.each(obj, fn). The function allows access to the object key of the current element.



      $.each(chessPieces, function(key, value) 

      //key = "p-w-1"
      //value = "role":"pawn", ...
      //this === value

      );





      share|improve this answer

























        7














        I'd use $.each(obj, fn). The function allows access to the object key of the current element.



        $.each(chessPieces, function(key, value) 

        //key = "p-w-1"
        //value = "role":"pawn", ...
        //this === value

        );





        share|improve this answer























          7












          7








          7






          I'd use $.each(obj, fn). The function allows access to the object key of the current element.



          $.each(chessPieces, function(key, value) 

          //key = "p-w-1"
          //value = "role":"pawn", ...
          //this === value

          );





          share|improve this answer












          I'd use $.each(obj, fn). The function allows access to the object key of the current element.



          $.each(chessPieces, function(key, value) 

          //key = "p-w-1"
          //value = "role":"pawn", ...
          //this === value

          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 31 '11 at 18:17









          John StricklerJohn Strickler

          20.6k44364




          20.6k44364





















              4














              for (var piece in chessPieces)
              alert(piece)






              share|improve this answer

























                4














                for (var piece in chessPieces)
                alert(piece)






                share|improve this answer























                  4












                  4








                  4






                  for (var piece in chessPieces)
                  alert(piece)






                  share|improve this answer












                  for (var piece in chessPieces)
                  alert(piece)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 31 '11 at 18:18









                  mplungjanmplungjan

                  87.1k21122181




                  87.1k21122181



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6191707%2fhow-to-get-name-of-current-json-property-while-iterating-through-them-in-javascr%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3

                      Museum of Modern and Contemporary Art of Trento and Rovereto