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

时间:2017-05-25 20:19:35

标签: javascript arrays

我有一个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添加到CopyTo命令的push部分的正确语法是什么?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

您可以尝试以下操作:

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