0

Here I want to push objects inside inner array of array. How can I do it?

ticketsToAdd = [];
ticketsToAdd.push({
    "TicketId": "",
    "Attendees": []
})

for(var i=0; i<5; i++) {
    ticketsToAdd['Attendees'].push({
                        "EmailID": "",
                        "Phone": "",
                        "FirstName": "",
                        "LastName": "",
                        "Company": ""                          
    })
}
Kevin Jimenez
  • 436
  • 2
  • 10
Shaik Matheen
  • 1,233
  • 15
  • 14

3 Answers3

5

You need an index for access an array element.

ticketsToAdd[0]['Attendees'].push();
//          ^^^

var ticketsToAdd = [],
    i;

ticketsToAdd.push({ TicketId: "", Attendees: [] });

for (i = 0; i < 5; i++) {
    ticketsToAdd[0]['Attendees'].push({ EmailID: "", Phone: "", FirstName: "", LastName: "", Company: "" });
}

console.log(ticketsToAdd);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You don't have an array inside an array.

You have an array inside an object inside an array.

You need to first access the object before you can access the array inside it.

ticketsToAdd[0].Attendees.push(...)
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

If you push only one time into ticketsToAdd array,

use,

for(var i=0; i<5; i++) {
        ticketsToAdd[0]['Attendees'].push({
        "EmailID": "",
        "Phone": "",
        "FirstName": "",
        "LastName": "",
        "Company": ""                          
        })  
    }

But, If you push multiple times, you have to use the index i

Since you are adding more objects into ticketsToAdd array, while inserting data into that array, use the number i from the iteration.

use ticketsToAdd.length to get the length first.

var ticketsToAdd = [];
ticketsToAdd.push({
    "TicketId": "",
    "Attendees": []
})

for(var i=0; i<ticketsToAdd.length; i++) {
  for(var y = 0; y<5; y++)
   {
      ticketsToAdd[i]['Attendees'].push({
        "EmailID": "",
        "Phone": "",
        "FirstName": "",
        "LastName": "",
        "Company": ""
       })                          
   }   
}

This gets all the objects from the array and pushes 5 times in each of it.

Sravan
  • 18,467
  • 3
  • 30
  • 54