How to push to an array in a particular position?

前端 未结 3 1131
闹比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:06

    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]
    

提交回复
热议问题