JavaScript: 如何在数组内的对象内引用数组并将某些内容推送给它?

JavaScript: How to reference an array inside an object inside an array and push something to it?

我有以下数组:

var ships = [ { locations: [], hits:0 },
              { different: [], different1:0 },
              { different3: [], different2:0 } ];

如何在第一个对象中引用数组 "locations" 并向其推送内容?谢谢!

您可以使用索引访问或解构赋值

索引访问

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)

应该是

ships[0].locations.push(newItem);