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.

Thanks.

link|improve this question
2  
The preferred way of creating a array in javascript is var employess = []; not var employees=new Array(); – Mattias Jakobsson Feb 12 '10 at 10:08
feedback

1 Answer

up vote 8 down vote accepted
var employees = {
    accounting: []
};

for(var i in someData) {

    var item = someData[i];

    employees.accounting.push({ 
        "firstName" : item.firstName,
        "lastName"  : item.lastName,
        "age"       : item.age 
    });
}
link|improve this answer
Cool, thx a lot! – sebasong Feb 12 '10 at 12:37
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.