How to push to an array in a particular position?

前端 未结 3 1130
闹比i
闹比i 2020-12-30 01:35

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 =         


        
3条回答
  •  离开以前
    2020-12-30 02:09

    To push any item at specific index in array use following syntax

    // The original array
    var array = ["one", "two", "four"];
    // splice(position, numberOfItemsToRemove, item)
    array.splice(2, 0, "three");
    
    console.log(array);  // ["one", "two", "three", "four"]
    

提交回复
热议问题