【发布时间】:2011-06-11 11:15:57
【问题描述】:
在以下 JSON 对象中:
var employees = { "accounting" : [ // accounting is an array in employees.
{ "firstName" : "John", // First element
"lastName" : "Doe",
"age" : 23 },
{ "firstName" : "Mary", // Second Element
"lastName" : "Smith",
"age" : 32 }
], // End "accounting" array.
"sales" : [ // Sales is another array in employees.
{ "firstName" : "Sally", // First Element
"lastName" : "Green",
"age" : 27 },
{ "firstName" : "Jim", // Second Element
"lastName" : "Galley",
"age" : 41 }
] // End "sales" Array.
} // End Employees
如何重组对象,以便可以像这样访问每个员工的名字:
employees[0].firstName
employees[1].firstName
// etc
【问题讨论】:
-
这不是 JSON 对象。它是对象文字表示法。 benalman.com/news/2010/03/theres-no-such-thing-as-a-json 。除此之外,是否要将
sales和accounting合并为一个数组? -
是的,我想要 1 个包含多个数组的数组。
-
在您的示例
employees[0].firstName中,您有一个对象数组。不是数组数组。你的意思是对象数组吗? -
是的,我需要一个对象数组。谢谢
标签: javascript arrays json