1

I want to push information to an array I have created. The thing is I want to push the info to be first in the array, or at index 0. How do I do this?

I want to avoid having to reverse the array to get the most recent first as this makes things much complicated later..

e.g, my array may look like:

{"entry1":"entry1-value"}, {"entry2":"entry2-value"}

and I want to push a new value to be first:

{"newEntry":"newEntry-value"},{"entry1":"entry1-value"},{"entry2":"entry2-value"}
j08691
  • 204,283
  • 31
  • 260
  • 272
rpsep2
  • 3,061
  • 10
  • 39
  • 52

2 Answers2

7

Use unshift():

array1.unshift({'newEntry' : 'newEntry-value'});

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
4

yourArray.unshift(newValue) pushes an item to the front of an array.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592