我有内部变量JSON对象是这样的:如何在JavaScript中迭代当前JSON属性的名称?
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"
},...
};
而且我迭代配线槽他们每个循环:
for (var piece in chessPieces){
//some code
}
我怎么会从这个获取当前块的名字吗?例如,我们当前位于第一个元素(piece = 0):chessPiece[piece].GiveMeTheName
==>这导致字符串“p-w-1”。
其实我打算当前元素传递到功能,因为我需要检查什么的,所以它看起来是这样的:
//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"
}
我也在我的项目中使用jQuery,所以如果那个图书馆有些东西可用,请告诉我。
在此先感谢! :)
的片(在... VAR件)本身拥有一块名 – mplungjan 2011-05-31 18:18:09
讨厌鬼。我在评论前真的失去了代表,我回答:( – mplungjan 2011-05-31 19:04:49