24

I'm trying to efficiently write a statement that pushes to position 1 of an array, and pushes whatever is in that position, or after it back a spot.

array = [4,5,9,6,2,5]

#push 0 to position 1

array = [4,0,5,9,6,2,5]

#push 123 to position 1

array = [4,123,0,5,9,6,2,5]

What is the best way to write this? (javascript or coffeescript acceptable)

Thanks!

fancy
  • 48,619
  • 62
  • 153
  • 231
  • you can use this http://stackoverflow.com/questions/586182/javascript-insert-item-into-array-at-a-specific-index – HasanCseBuet Jan 06 '12 at 07:07
  • 1
    http://www.w3schools.com/jsref/jsref_splice.asp – Anand Shah Jan 06 '12 at 07:12
  • 3
    @Anand: Please don't feed w3fools.com any more hits, there are much [better](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice) references [available](http://es5.github.com/#x15.4.4.12). – mu is too short Jan 06 '12 at 07:21
  • 1
    Possible duplicate of [How to insert an item into an array at a specific index?](http://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index) – Rodrigo Salgado Atala Apr 28 '17 at 20:20

3 Answers3

40
array = [4,5,9,6,2,5]

#push 0 to position 1
array.splice(1,0,0)

array = [4,0,5,9,6,2,5]

#push 123 to position 1
array.splice(1,0,123)

array = [4,123,0,5,9,6,2,5]
fancy
  • 48,619
  • 62
  • 153
  • 231
  • 3
    **Note:** splice returns an array of the _removed_ elements. In these examples, no elements are removed so `console.log(array.splice(1,0,123))` will return `[]`. Should have RTFM – Pakman Jul 14 '14 at 21:12
  • [The manual](http://www.w3schools.com/jsref/jsref_splice.asp) also says (as [Vlada](https://stackoverflow.com/a/29098872/4575793) and [user12838586 pointed out](https://stackoverflow.com/a/60868093/4575793)) `splice(position, numberOfItemsToRemove, item)`, especially explaining the second parameter. – Cadoiz Mar 06 '23 at 10:55
4

The splice() function is the only native array function that lets you add elements to the specific place of an array

I will get a one array that you entered in your question to describe

splice(position, numberOfItemsToRemove, item)
  • position = What is the position that you want to add new item
  • numberOfItemsToRemove = This indicate how many number of items will deleted. That's mean delete will start according to the position that new item add.

Ex = if you want to add 1 position to 123 result will be like this ([4,123,0,5,9,6,2,5]) but if you give numberOfItemsToRemove to 1 it will remove first element after the 123 if you give 2 then its delete two element after 123.

  • item = the new item that you add

function my_func(){
        var suits = [4,0,5,9,6,2,5]
        
        suits.splice(1 , 0 , 123);

        document.getElementById('demo').innerHTML = suits;
}
<p id="demo">4,0,5,9,6,2,5</p>
<button id="btn01" onclick="my_func()">Splice Array</button>
Oskar Austegard
  • 4,599
  • 4
  • 36
  • 50
  • Additional info: [you can find the manual here](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice) – Cadoiz Mar 06 '23 at 10:58
0

Could you not just do:

myArray[idx] = foo;

You will need to have an array with the appropriate size, but I'm not sure what the behavior for splice on a list that doesn't have the requested index is either.