17

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
  • 5,442
  • 8
  • 47
  • 78

1 Answers1

18

What you want is the splice function:

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

vittore
  • 17,449
  • 6
  • 44
  • 82
CoderPi
  • 12,985
  • 4
  • 34
  • 62