12

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!

Aral Roca
  • 4,848
  • 7
  • 45
  • 77

1 Answers1

13

What you want is the splice function:

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

vittore
  • 17,223
  • 6
  • 43
  • 82
CoderPi
  • 12,423
  • 4
  • 31
  • 59