0 votes
1 view
in Web Technology by (6.8k points)

How do I create a JavaScript array dynamically?

1 Answer

0 votes
by (13k points)

Consider the array of objects:

var someData = [

   {firstName: "Max", lastName: "Mustermann", age: 40},

   {firstName: "Hagbard", lastName: "Celine", age: 44},

   {firstName: "Karl", lastName: "Koch", age: 42},

];

Using 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 by using Array.prototype.map():

var employees = {

    accounting: []

};

someData.map(function(item) {        

   employees.accounting.push({ 

        "firstName" : item.firstName,

        "lastName"  : item.lastName,

        "age"       : item.age 

    });

}

Want to be a Full Stack Developer? Check out the Full Stack Developer course from Intellipaat.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...