3

I have this array.

var array = [
 {
  title:'Fish'
  id: 1
 },
 {
  title: 'Meat'
  id:2
 }
]

It's used in a drop down. I'd like to add an option at the top:

{
  title:'All'
  id:3
}

How do I push this object to the beginning of the array? If I use push, then it's placed at the end.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Joe
  • 4,274
  • 32
  • 95
  • 175

1 Answers1

3

Use the unshift method of the javascript array:

array.unshift({title: 'All', id: 3})
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Bogdan Iulian Bursuc
  • 2,215
  • 1
  • 16
  • 19