Reputation: 27
I have the following array:
var ships = [ { locations: [], hits:0 },
{ different: [], different1:0 },
{ different3: [], different2:0 } ];
How do I reference the array "locations" inside the 1st object and push something to it? Thanks!
Upvotes: 0
Views: 54
Reputation: 33726
You can use index access or Destructuring assignment
Index access
var ships = [ { locations: [], hits:0 },
{ different: [], different1:0 },
{ different3: [], different2:0 } ];
ships[0].locations.push("Ele from SO");
console.log(ships)
Destructuring assignment
var ships = [ { locations: [], hits:0 },
{ different: [], different1:0 },
{ different3: [], different2:0 } ];
var [obj] = ships;
obj.locations.push("Ele from SO");
console.log(ships)
Upvotes: 1