简体   繁体   中英

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. However, I can't seem to correctly add another array of items into the parent object 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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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