如何使用 JS 获取具有基本数组顺序元素的数组:
baseArray = [1,2,3,4,5,6,7,8]
newArray1 = [1,4,7]
newArray2 = [2,5,8]
newArray3 = [3,6]
最好的方法是什么
最佳答案
您可以利用 reminder operator %
的力量用于获取正确的索引和目标数组。
var array = [1, 2, 3, 4, 5, 6, 7, 8],
array1 = [], array2 = [], array3 = [];
array.reduce((r, v, i) => (r[i % 3].push(v), r), [array1, array2, array3]);
console.log(array1);
console.log(array2);
console.log(array3);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 使用 javascript 按特定顺序创建新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52593888/