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"}
1
  • Have you tried anything? Commented Aug 6, 2013 at 15:30

2 Answers 2

7

Use unshift():

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

JS Fiddle demo.

References:

Sign up to request clarification or add additional context in comments.

Comments

4

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.