How to add a new object (key-value pair) to an array in javascript? How to add a new object (key-value pair) to an array in javascript? arrays arrays

How to add a new object (key-value pair) to an array in javascript?


.push() will add elements to the end of an array.

Use .unshift() if need to add some element to the beginning of array i.e:

items.unshift({'id':5});

Demo:

items = [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}];items.unshift({'id': 0});console.log(items);