Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append to a JavaScript array dynamically? [duplicate]

Tags:

javascript

Possible Duplicate:
How do I create Javascript array(JSON format) dynamically?

I am attempting to 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 out with:

 var employees=new Array();

How do I continue to append to the array dynamically (might change firstName with variable)?

like image 844
Michael Anderson Avatar asked Aug 22 '12 06:08

Michael Anderson


1 Answers

var employees = {accounting: []};

employees.accounting.push({
    "firstName" : "New",
    "lastName"  : "Employee",
    "age"       : 18
});
like image 71
Lucas Green Avatar answered Sep 20 '22 07:09

Lucas Green