如何在Javascript中将项目数组上移4位?

我有以下字符串数组:

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

我需要一个函数将“array1”转换为:
// Note how "t0" moves to the fourth position for example
var array2 = ["t3","t4","t5","t0","t1","t2"];

提前致谢。

最佳答案

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

在Firebug中运行以下命令进行验证
var array1 = ["t0","t1","t2","t3","t4","t5"];
console.log(array1);
array1 = array1.concat(array1.splice(0,3));
console.log(array1);

结果是
["t0", "t1", "t2", "t3", "t4", "t5"]
["t3", "t4", "t5", "t0", "t1", "t2"]

关于javascript - 如何在Javascript中将项目数组上移4个位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1521071/

10-13 03:47