0

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
  • 197,815
  • 30
  • 248
  • 265
rpsep2
  • 2,901
  • 10
  • 34
  • 50

2 Answers2

6

Use unshift():

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

JS Fiddle demo.

References:

David Thomas
  • 240,457
  • 50
  • 366
  • 401
4

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

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566