将 Array 对象拆分为夹头
Split Array object into chucks
我在 JS 中有这个数组
initialArray = [A,B,C,C,D,E,F,F,G,K]
我想分成:
chucks = [[A,B,C], [C,D,E,F], [F,G,K]]
重复项是分隔符,例如 'C' 或 'F'
如何在 ES6 中进行拆分?
您可以减少数组并添加值或新数组,具体取决于最后一个值。
const
array = ['A','B','C','C','D','E','F','F','G','K'],
result = array.reduce((r, v, i, { [i - 1]: last }) => {
if (v === last) r.push([]);
r[r.length - 1].push(v);
return r;
}, [[]]);
console.log(result);
已编辑:现在基于克隆
const initialArray = ["A","B","C","C","D","E","F","F","G","K"];
const chunks = [];
let smallChunk = [];
let index = 0;
while (index < initialArray.length){
smallChunk.push(initialArray[index]);
if (initialArray[index+1] == initialArray[index]){
chunks.push(smallChunk);
smallChunk = [];
}
index++;
}
chunks.push(smallChunk);
console.log(chunks);
使用单个 for
循环并将当前块保存在要推送到的变量中的相当直接的解决方案。
const initialArray = ['A', 'A', 'B', 'C', 'C', 'D', 'E', 'F', 'F', 'G', 'K', 'K'];
const chunk_at_duplicate = (arr) => {
let chunk = [], res = [chunk];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === arr[i - 1]) {
res.push(chunk = []);
}
chunk.push(arr[i]);
}
return res;
};
console.log(chunk_at_duplicate(initialArray));
您可以将 for ... of
与 initialArray.entries()
一起使用,如下所示:
const initialArray = ["A","B","C","C","D","E","F","F","G","K"];
const chunks = [];
let start = 0
for(const [i,v] of initialArray.entries()) {
if(i > 0 && v === initialArray[i-1]) {
chunks.push( initialArray.slice(start,i) );
start = i;
} else if( i === initialArray.length - 1 ) {
chunks.push( initialArray.slice(start) );
}
}
console.log( chunks );
我在 JS 中有这个数组
initialArray = [A,B,C,C,D,E,F,F,G,K]
我想分成:
chucks = [[A,B,C], [C,D,E,F], [F,G,K]]
重复项是分隔符,例如 'C' 或 'F'
如何在 ES6 中进行拆分?
您可以减少数组并添加值或新数组,具体取决于最后一个值。
const
array = ['A','B','C','C','D','E','F','F','G','K'],
result = array.reduce((r, v, i, { [i - 1]: last }) => {
if (v === last) r.push([]);
r[r.length - 1].push(v);
return r;
}, [[]]);
console.log(result);
已编辑:现在基于克隆
const initialArray = ["A","B","C","C","D","E","F","F","G","K"];
const chunks = [];
let smallChunk = [];
let index = 0;
while (index < initialArray.length){
smallChunk.push(initialArray[index]);
if (initialArray[index+1] == initialArray[index]){
chunks.push(smallChunk);
smallChunk = [];
}
index++;
}
chunks.push(smallChunk);
console.log(chunks);
使用单个 for
循环并将当前块保存在要推送到的变量中的相当直接的解决方案。
const initialArray = ['A', 'A', 'B', 'C', 'C', 'D', 'E', 'F', 'F', 'G', 'K', 'K'];
const chunk_at_duplicate = (arr) => {
let chunk = [], res = [chunk];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === arr[i - 1]) {
res.push(chunk = []);
}
chunk.push(arr[i]);
}
return res;
};
console.log(chunk_at_duplicate(initialArray));
您可以将 for ... of
与 initialArray.entries()
一起使用,如下所示:
const initialArray = ["A","B","C","C","D","E","F","F","G","K"];
const chunks = [];
let start = 0
for(const [i,v] of initialArray.entries()) {
if(i > 0 && v === initialArray[i-1]) {
chunks.push( initialArray.slice(start,i) );
start = i;
} else if( i === initialArray.length - 1 ) {
chunks.push( initialArray.slice(start) );
}
}
console.log( chunks );