Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

How do I create a JavaScript array dynamically?

1 Answer

0 votes
by (13.1k 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

0 votes
0 answers
0 votes
1 answer

Browse Categories

...