如何在数组数组中推送对象

如何在数组数组中推送对象

本文介绍了如何在数组数组中推送对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想将对象推入array的内部数组中.我该怎么做?

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": ""
    })
}

推荐答案

如果仅将一次压入 ticketsToAdd 数组,

If you push only one time into ticketsToAdd array,

使用

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

但是,如果您多次按下,则必须使用索引 i

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

由于要在 ticketsToAdd 数组中添加更多对象,因此在将数据插入该数组中时,请使用迭代中的数字 i .

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

使用 ticketsToAdd.length 首先获取长度.

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": ""
       })
   }
}

这将从数组中获取所有对象,并在每个数组中压入5次.

这篇关于如何在数组数组中推送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:06