如何dynamic创buildJavaScript数组(JSON格式)?

我正在尝试创build以下内容:

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. } // End Employees 

我开始

  var employees = new Array(); 

我如何继续dynamic创build数组(可能会改变firstName与variables)? 我似乎没有得到嵌套的数组权利。

 var employees = { accounting: [] }; for(var i in someData) { var item = someData[i]; employees.accounting.push({ "firstName" : item.firstName, "lastName" : item.lastName, "age" : item.age }); } 

Array.prototype.map()

 var employees = { accounting: [] }; // someData has to be an array someData.map(function(item) { employees.accounting.push({ "firstName" : item.firstName, "lastName" : item.lastName, "age" : item.age }); } 
 var accounting = []; var employees = {}; for(var i in someData) { var item = someData[i]; accounting.push({ "firstName" : item.firstName, "lastName" : item.lastName, "age" : item.age }); } employees.accounting = accounting; 

我所做的只是与@Chase有点不同而已。

 var employees = {}; // ...and then: employees.accounting = new Array(); for (var i = 0; i < someArray.length; i++) { var temp_item = someArray[i]; // Maybe, here make something like: // temp_item.name = 'some value' employees.accounting.push({ "firstName" : temp_item.firstName, "lastName" : temp_item.lastName, "age" : temp_item.age }); } 

那工作形成了我!

我希望它可以对其他一些身体有用!

 var student = []; var obj = { 'first_name': name, 'last_name': name, 'age': age, } student.push(obj);