Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  112] [ 5]  / answers: 1 / hits: 60434  / 13 Years ago, fri, january 6, 2012, 12:00:00

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!


More From » arrays

 Answers
14
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]

[#88194] Thursday, January 5, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;