Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  57] [ 5]  / answers: 1 / hits: 19519  / 15 Years ago, mon, october 5, 2009, 12:00:00

How do I shift an array of items up by 4 places in Javascript?



I have the following string array:



var array1 = [t0,t1,t2,t3,t4,t5];


I need a function convert array1 to result in:



// Note how t0 moves to the fourth position for example
var array2 = [t3,t4,t5,t0,t1,t2];


Thanks in advance.


More From » arrays

 Answers
70
array1 = array1.concat(array1.splice(0,3));


run the following in Firebug to verify



var array1 = [t0,t1,t2,t3,t4,t5];
console.log(array1);
array1 = array1.concat(array1.splice(0,3));
console.log(array1);


results in



[t0, t1, t2, t3, t4, t5]
[t3, t4, t5, t0, t1, t2]

[#98569] Tuesday, September 29, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shane

Total Points: 239
Total Questions: 91
Total Answers: 114

Location: Faroe Islands
Member since Tue, Jul 7, 2020
4 Years ago
shane questions
;