[英]How can I put an array of objects into another object?
I have a JavaScript array, copyCommands
, that I want to push some items into. 我有一个JavaScript数组
copyCommands
,我想将其中的一些项目推入其中。 However, I can't seem to correctly add another array of items into the parent object autoGenData
. 但是,我似乎无法正确地将另一个项目数组添加到父对象
autoGenData
。
//autoGenData is the object, copyCommands is an array
autoGenData.copyCommands.push({
CopyFrom: UnitFrom,
//CopyTo: //array that needs to hold CopyOptions and Unit
});
//Need to add these into CopyTo
//CopyOptions: checkedItems,
//Unit: localUnitTo
What's the proper syntax for adding CopyOptions
and unit
to the CopyTo
portion of the push
command? 将
CopyOptions
和unit
添加到push
命令的CopyTo
部分的正确语法是什么?
I think you are looking for something like this: 我认为您正在寻找这样的东西:
autoGenData.copyCommands.push({
CopyFrom: UnitFrom,
CopyTo: {
CopyOptions: checkedItems,
Unit: localUnitTo
}
});
You can try the following: 您可以尝试以下方法:
var x = [1, 2, 3, 4];
var y = [5, 6, 7];
x.push(...y);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.