0

Hi so i got this array:

  var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1] 
];

and i want to append to it just like it is example:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1],
  ['New item', -22.950198, 141.259302, 6]
];

I have no idea how to do it, search 'Add items to array in array ' didn't quite seem to work out.

3
  • 3
    Array.prototype.push Commented Mar 21, 2018 at 12:22
  • 3
    locations.push(['New item', -22.950198, 141.259302, 6]); Commented Mar 21, 2018 at 12:22
  • Your last comment isn't correct. You are not adding items to array in array. You are simply adding to an array. You can push any item it just so happens this item is an array. But if you wanted to add items to an array inside an array you would locate the array to add to and push as well. So would be like: locations[1].push(whatever). This would push whatever to the end of the 'Coogee Beach' array. Commented Mar 21, 2018 at 12:27

5 Answers 5

1
var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1] 
];

var newLocation = ['New item', -22.950198, 141.259302, 6];

locations.push(newLocation);
Sign up to request clarification or add additional context in comments.

Comments

1

locations.push( ['New item', -22.950198, 141.259302, 6]); ist the right answer.

Comments

0

you can use push() method if you want to add on the last ! or you can add in the first by using .unshift method ! look here for more informations https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Comments

0

Try to use this:

 var locations = [
 {'Bondi Beach', -33.890542, 151.274856, 4},
 {'Coogee Beach', -33.923036, 151.259052, 5},
 {'Cronulla Beach', -34.028249, 151.157507, 3},
 {'Manly Beach', -33.80010128657071, 151.28747820854187, 2},
 {'Maroubra Beach', -33.950198, 151.259302, 1}

];

Now you can apply a push

locations.push({'New item', -22.950198, 141.259302, 6});

Let me know if it work.

1 Comment

You are pushing an object; not array.
0

or you can use spread syntax

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1] 
];

locations = [...locations,['New item', -22.950198, 141.259302, 6]];

console.log(locations);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.