我有以下数组:
var ships = [ { locations: [], hits:0 },
{ different: [], different1:0 },
{ different3: [], different2:0 } ];
如何在第一个对象内引用数组“位置”并将其压入其中?谢谢!
最佳答案
您可以使用索引访问或解构分配
索引访问
var ships = [ { locations: [], hits:0 },
{ different: [], different1:0 },
{ different3: [], different2:0 } ];
ships[0].locations.push("Ele from SO");
console.log(ships)
销毁工作
var ships = [ { locations: [], hits:0 },
{ different: [], different1:0 },
{ different3: [], different2:0 } ];
var [obj] = ships;
obj.locations.push("Ele from SO");
console.log(ships)
关于javascript - JavaScript:如何在数组中的对象内引用数组并将其插入其中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49264847/