admin管理员组文章数量:1389772
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": ""
})
}
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": ""
})
}
Share
Improve this question
edited Feb 2, 2017 at 9:52
Kevin Jimenez
4462 silver badges10 bronze badges
asked Feb 2, 2017 at 9:27
Shaik MatheenShaik Matheen
1,30715 silver badges15 bronze badges
1
-
1
Change
ticketsToAdd['Attendees'].push(
toticketsToAdd[i]['Attendees'].push(
– Shubham Commented Feb 2, 2017 at 9:29
3 Answers
Reset to default 5You 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; }
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(...)
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.
本文标签: javascriptHow to Push object in an array of arrayStack Overflow
版权声明:本文标题:javascript - How to Push object in an array of array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744730222a2622004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论