Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Unshift to second position of the array [duplicate]

I understand this diagram:

Unshift/Push diagram

But now my question is... How can I add an element just in the second position of the Array?

If I have this array: (A, C, G, T) and I want to add B...

The result that I want should be: (A, B, C, G, T)

Any suggestions?

Thank you!

like image 571
Aral Roca Avatar asked Nov 26 '15 21:11

Aral Roca


1 Answers

What you want is the splice function:

arr.splice(index, 0, item); will insert item into arr at the specified index.

like image 144
CoderPi Avatar answered Sep 21 '22 00:09

CoderPi