How do I create JavaScript array (JSON format) dynamically?

JavascriptJson

Javascript Problem Overview


I am trying the create the following:

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

I started with

 var employees = new Array();

How do I continue to create the array dynamically (might change firstName with variable)? I don't seem to get the nested array right.

Javascript Solutions


Solution 1 - Javascript

Our array of objects

var someData = [
   {firstName: "Max", lastName: "Mustermann", age: 40},
   {firstName: "Hagbard", lastName: "Celine", age: 44},
   {firstName: "Karl", lastName: "Koch", age: 42},
];

with for...in

var employees = {
    accounting: []
};

for(var i in someData) {    

    var item = someData[i];   

    employees.accounting.push({ 
        "firstName" : item.firstName,
        "lastName"  : item.lastName,
        "age"       : item.age 
    });
}

or with Array.prototype.map(), which is much cleaner:

var employees = {
    accounting: []
};

someData.map(function(item) {        
   employees.accounting.push({ 
        "firstName" : item.firstName,
        "lastName"  : item.lastName,
        "age"       : item.age 
    });
}

Solution 2 - Javascript

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;

Solution 3 - Javascript

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

Solution 4 - Javascript

What I do is something just a little bit different from @Chase answer:

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
	});
}

And that work form me!

I hope it could be useful for some body else!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsebas23View Question on Stackoverflow
Solution 1 - JavascriptAlexView Answer on Stackoverflow
Solution 2 - JavascriptChaseView Answer on Stackoverflow
Solution 3 - JavascriptVenu UpadhyayView Answer on Stackoverflow
Solution 4 - JavascriptalexventuraioView Answer on Stackoverflow