How to get name of current JSON property while iterating through them in JavaScript?
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
add a comment |
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
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
add a comment |
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
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
javascript jquery json for-loop each
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
Thanks, I was confused since chrome console gave me undefined when typing inchessPieces[1]
– Happy
May 31 '11 at 18:24
1
It'schessPieces
, 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
add a comment |
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
);
add a comment |
for (var piece in chessPieces)
alert(piece)
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thanks, I was confused since chrome console gave me undefined when typing inchessPieces[1]
– Happy
May 31 '11 at 18:24
1
It'schessPieces
, 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
add a comment |
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
Thanks, I was confused since chrome console gave me undefined when typing inchessPieces[1]
– Happy
May 31 '11 at 18:24
1
It'schessPieces
, 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
add a comment |
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
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
answered May 31 '11 at 18:18
PwnnaPwnna
4,104155282
4,104155282
Thanks, I was confused since chrome console gave me undefined when typing inchessPieces[1]
– Happy
May 31 '11 at 18:24
1
It'schessPieces
, 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
add a comment |
Thanks, I was confused since chrome console gave me undefined when typing inchessPieces[1]
– Happy
May 31 '11 at 18:24
1
It'schessPieces
, 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
add a comment |
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
);
add a comment |
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
);
add a comment |
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
);
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
);
answered May 31 '11 at 18:17
John StricklerJohn Strickler
20.6k44364
20.6k44364
add a comment |
add a comment |
for (var piece in chessPieces)
alert(piece)
add a comment |
for (var piece in chessPieces)
alert(piece)
add a comment |
for (var piece in chessPieces)
alert(piece)
for (var piece in chessPieces)
alert(piece)
answered May 31 '11 at 18:18
mplungjanmplungjan
87.1k21122181
87.1k21122181
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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