0

This is the array of objects employees which contains accounting and sales arrays. Is it possible to add new entry to both the accounting and sales array in a single push?

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.                                  
       sales: [ // Sales is another array in employees.
       {
            "firstName": "Sally", // First Element
            "lastName" : "Green",
            "age"      : 27
       },

       {
            "firstName": "Jim", // Second Element
            "lastName" : "Galley",
            "age"      : 41
       }
      ] // End "sales" Array.
} // End Employees
4
  • employees.accounting.push(...)? What is the problem? Can you show us the for loop you're trying to use? Commented Sep 18, 2014 at 19:31
  • I just worried about picking from three different array to create a single object. Nothing was wrong with the push. for(i=0;i<arrayfirstName.length;i++) { employees.accounting.push(arrayfirstName[i]); employees.accounting.push(arraylastName[i]); employees.accounting.push(arrayage[i]); } Commented Sep 18, 2014 at 19:35
  • Ok, I don't know how we were supposed to guess that when you never told us about arrayfirstname, arraylastname, and arrayage. Please strive to include all relevant information in the questions you ask. Commented Sep 18, 2014 at 19:38
  • 1
    I presume the initial close vote was due to the fact that your post contains neither a question nor any mention of any problem. And your deletion of that part at the end removed any possibility of deciphering what you were trying to ask. Commented Sep 18, 2014 at 19:47

1 Answer 1

2

Here's one way you can approach this: http://jsfiddle.net/bmartinelle/hrgxc3r7/

for(i=0;i< arrayfirstName.length; i++)
{
       var newEntry = {"firstName" : arrayfirstName[i], "lastName" : arraylastName[i], "age" :         arrayage[i] };

       employees.accounting.push(newEntry);
}

You have to create a JSON entry to push to your JSON structure - then you can add that JSON Object to your accounting Array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.