假设在“人类”对象中有多个嵌套对象(human1,human2,human3)。在不同数据选项之间循环迭代对象
human: {
"human1": {
"name" : "John",
"sex" : "male",
"age" : 18
}
"human2": {
"name" : "Peter",
"sex" : "male",
"age" : 16
}
"human3": {
"name" : "May",
"sex" : "female",
"age" : 19
}
}
而且我有一个名为currentPlayer下面另一个对象,我希望它是一个容器,以便从“human1”,“human2”,或“human3”为不同的目的而使用的数据。
currentPlayer: {
"name" : "default",
"sex" : "default",
"age" : 0
}
例子:今天我要currentPlayer是约翰,它会
currentPlayer: {
"name" : "John",
"sex" : "male",
"age" : 18
}
然后我想currentPlayer是彼得,有云:
currentPlayer: {
"name" : "Peter",
"sex" : "male",
"age" : 16
}
我如何用loop循环迭代currentPlayer的属性值,而不仅仅是一个接一个地键入?谢谢...
这真的不清楚你在问什么?你是问在第一个数据结构中如何用'name:“John”'来查找对象?另外,请显示完整的对象定义,而不仅仅是部分对象定义。您显示为'currentPlayer'的只是某个父对象中的一个属性。 – jfriend00
我想你可能会要求类似jQuery.extend()的东西。在youmightnotneedjquery.com上还有一个非jQuery实现 – JonSG
我想只在我现有的currentPlayer对象中迭代来自human1或human2或human3的属性值,对不起没有解释清楚... –