繁体   English   中英

如何将对象数组放入另一个对象?

[英]How can I put an array of objects into another object?

我有一个JavaScript数组copyCommands ,我想将其中的一些项目推入其中。 但是,我似乎无法正确地将另一个项目数组添加到父对象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

CopyOptionsunit添加到push命令的CopyTo部分的正确语法是什么?

我认为您正在寻找这样的东西:

autoGenData.copyCommands.push({
  CopyFrom: UnitFrom,
  CopyTo: {
    CopyOptions: checkedItems,
    Unit: localUnitTo
  }
});

您可以尝试以下方法:

var x = [1, 2, 3, 4];
var y = [5, 6, 7];
x.push(...y);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM