拆分具有特定块大小的数组
Split an Array with specific chunks size
例如我有一个块数组,这个数组有各个块的大小。
let example = [3,3]; // Chunks array
let auxarrayindex = [1,2,3,4,5,6]; // Array that I want to splice
let example2 = [3,2,3]; // Chunks array
let auxarrayindex2 = [1,2,3,4,5,6,7,8]; // Array that I want to splice
我想要的结果是:
[1,2,3],[4,5,6] and the second [1,2,3],[4,5],[6,7,8]
这是我的代码:
for (let auxexample = 0; auxexample < example.length; auxexample++) {
finalauxarray.push(auxarrayindex.slice(0, example[auxexample]));
}
我的代码的结果是:
[1,2,3],[1,2,3] and the second [1,2,3],[1,2],[1,2,3]
问题是您的切片总是从相同的索引 (0) 开始。
使用一个变量(如 i
),当你获取块时增加:
let example = [3,2,3];
let auxarrayindex = [1,2,3,4,5,6,7,8];
let finalauxarray = [];
let i = 0;
for (let auxexample = 0; auxexample < example.length; auxexample++) {
finalauxarray.push(auxarrayindex.slice(i, i+=example[auxexample]));
}
console.log(finalauxarray);
您也可以使用 map
作为循环:
let example = [3,2,3];
let auxarrayindex = [1,2,3,4,5,6,7,8];
let i = 0;
let finalauxarray = example.map(size => auxarrayindex.slice(i, i+=size));
console.log(finalauxarray);
使用 splice
而不是 slice
的工作示例,因为我认为它为这个特定的用例提供了一个更清晰的 API:
let example = [3, 3];
let auxArrayIndex = [1, 2, 3, 4, 5, 6];
let example2 = [3, 2, 3];
let auxArrayIndex2 = [1, 2, 3, 4, 5, 6, 7, 8];
function getChunks(chunkSizes, array) {
let result = [];
for (let chunkSize of chunkSizes) {
result.push(array.splice(0, chunkSize));
}
return result;
}
let chunks = getChunks(example, auxArrayIndex);
let chunks2 = getChunks(example2, auxArrayIndex2);
console.log(chunks); // logs "[1,2,3], [4,5,6]"
console.log(chunks2); // logs "[1,2,3], [4,5], [6,7,8]"
问题是因为切片参数错误
您可以详细了解 slice 如何在此 link
上工作
https://www.w3schools.com/jsref/jsref_slice_array.asp
第一个参数是起始位置,最后一个参数是结果中不包含的结束位置
你也可以为此使用拼接
https://www.w3schools.com/jsref/jsref_splice.asp
希望对您有所帮助
例如我有一个块数组,这个数组有各个块的大小。
let example = [3,3]; // Chunks array
let auxarrayindex = [1,2,3,4,5,6]; // Array that I want to splice
let example2 = [3,2,3]; // Chunks array
let auxarrayindex2 = [1,2,3,4,5,6,7,8]; // Array that I want to splice
我想要的结果是:
[1,2,3],[4,5,6] and the second [1,2,3],[4,5],[6,7,8]
这是我的代码:
for (let auxexample = 0; auxexample < example.length; auxexample++) {
finalauxarray.push(auxarrayindex.slice(0, example[auxexample]));
}
我的代码的结果是:
[1,2,3],[1,2,3] and the second [1,2,3],[1,2],[1,2,3]
问题是您的切片总是从相同的索引 (0) 开始。
使用一个变量(如 i
),当你获取块时增加:
let example = [3,2,3];
let auxarrayindex = [1,2,3,4,5,6,7,8];
let finalauxarray = [];
let i = 0;
for (let auxexample = 0; auxexample < example.length; auxexample++) {
finalauxarray.push(auxarrayindex.slice(i, i+=example[auxexample]));
}
console.log(finalauxarray);
您也可以使用 map
作为循环:
let example = [3,2,3];
let auxarrayindex = [1,2,3,4,5,6,7,8];
let i = 0;
let finalauxarray = example.map(size => auxarrayindex.slice(i, i+=size));
console.log(finalauxarray);
使用 splice
而不是 slice
的工作示例,因为我认为它为这个特定的用例提供了一个更清晰的 API:
let example = [3, 3];
let auxArrayIndex = [1, 2, 3, 4, 5, 6];
let example2 = [3, 2, 3];
let auxArrayIndex2 = [1, 2, 3, 4, 5, 6, 7, 8];
function getChunks(chunkSizes, array) {
let result = [];
for (let chunkSize of chunkSizes) {
result.push(array.splice(0, chunkSize));
}
return result;
}
let chunks = getChunks(example, auxArrayIndex);
let chunks2 = getChunks(example2, auxArrayIndex2);
console.log(chunks); // logs "[1,2,3], [4,5,6]"
console.log(chunks2); // logs "[1,2,3], [4,5], [6,7,8]"
问题是因为切片参数错误 您可以详细了解 slice 如何在此 link
上工作https://www.w3schools.com/jsref/jsref_slice_array.asp
第一个参数是起始位置,最后一个参数是结果中不包含的结束位置
你也可以为此使用拼接 https://www.w3schools.com/jsref/jsref_splice.asp
希望对您有所帮助