如何仅 flatten/reduce 最深或以其他方式指定的第 N 维数组级别
How to flatten/reduce only the deepest or otherwise specicified level of an Nth-dimensional array
假设一个N维数组,例如
const array = [
"1",
["2","2"],
[["3","3"],["3","3"]],
[
[
[["4","4"],"3"],
[["4","4"],"3"]
],
],
[["3","3"],["3","3"]],
["2","2"],
"1"
];
我只能找到从索引的浅端向上展平数组的方法,但我需要 flatten/reduce 或以其他方式仅处理最深(或任意)索引级别。
当 运行 最深层次时,我正在寻找一个数组输出类似
的东西
array = [
"1",
["2","2"],
[["3","3"],["3","3"]],
[
[
["4,4","3"],
["4,4","3"]
],
],
[["3","3"],["3","3"]],
["2","2"],
"1"
];
我找不到不是垃圾的解决方案...(over-complicated/incredibly 乱七八糟)
任何帮助将不胜感激
您可以使用递归函数获取数组和所需级别,然后在级别为 1 时停止重复出现,此时您只需过滤掉该(子)数组中的非数组值:
function extractLevel(arr, level) {
return level <= 1
? arr.filter(val => !Array.isArray(val))
: arr.filter(Array.isArray)
.flatMap(arr => extractLevel(arr, level-1));
}
// demo
const array = ["1",["2","2"],[["3","3"],["3","3"]],[[[["4","4"],"3"],[["4","4"],"3"]],[["",""],["",""]]],[["3","3"],["3","3"]],["2","2"],"1"];
// extract the values (not arrays) at level 3:
console.log(extractLevel(array, 3));
您可以创建一个函数来获取您想要展平的数据和级别,并且只会展平该级别。然后要获得最后一个级别,您可以创建另一个函数。
const array = ["1",["2","2"],[["3","3"],["3","3"]],[[[["4","4"],"3"],[["4","4"],"3"]],[["",""],["",""]]],[["3","3"],["3","3"]],["2","2"],"1"];
function lastLvl(data, lvl = 0) {
return data.reduce((r, e) => {
if (lvl > r) r = lvl
if (Array.isArray(e)) {
const nlvl = lastLvl(e, lvl + 1);
if (nlvl > r) r = nlvl
}
return r
}, 0)
}
function flattenLvl(data, lvl, clvl = 1) {
return data.reduce((r, e) => {
if (Array.isArray(e)) {
const nested = flattenLvl(e, lvl, clvl + 1);
if (clvl == lvl) r.push(...nested);
else r.push(nested)
} else {
r.push(e)
}
return r;
}, [])
}
const lvl = lastLvl(array)
const result = flattenLvl(array, lvl)
console.log(result)
假设一个N维数组,例如
const array = [
"1",
["2","2"],
[["3","3"],["3","3"]],
[
[
[["4","4"],"3"],
[["4","4"],"3"]
],
],
[["3","3"],["3","3"]],
["2","2"],
"1"
];
我只能找到从索引的浅端向上展平数组的方法,但我需要 flatten/reduce 或以其他方式仅处理最深(或任意)索引级别。
当 运行 最深层次时,我正在寻找一个数组输出类似
的东西array = [
"1",
["2","2"],
[["3","3"],["3","3"]],
[
[
["4,4","3"],
["4,4","3"]
],
],
[["3","3"],["3","3"]],
["2","2"],
"1"
];
我找不到不是垃圾的解决方案...(over-complicated/incredibly 乱七八糟) 任何帮助将不胜感激
您可以使用递归函数获取数组和所需级别,然后在级别为 1 时停止重复出现,此时您只需过滤掉该(子)数组中的非数组值:
function extractLevel(arr, level) {
return level <= 1
? arr.filter(val => !Array.isArray(val))
: arr.filter(Array.isArray)
.flatMap(arr => extractLevel(arr, level-1));
}
// demo
const array = ["1",["2","2"],[["3","3"],["3","3"]],[[[["4","4"],"3"],[["4","4"],"3"]],[["",""],["",""]]],[["3","3"],["3","3"]],["2","2"],"1"];
// extract the values (not arrays) at level 3:
console.log(extractLevel(array, 3));
您可以创建一个函数来获取您想要展平的数据和级别,并且只会展平该级别。然后要获得最后一个级别,您可以创建另一个函数。
const array = ["1",["2","2"],[["3","3"],["3","3"]],[[[["4","4"],"3"],[["4","4"],"3"]],[["",""],["",""]]],[["3","3"],["3","3"]],["2","2"],"1"];
function lastLvl(data, lvl = 0) {
return data.reduce((r, e) => {
if (lvl > r) r = lvl
if (Array.isArray(e)) {
const nlvl = lastLvl(e, lvl + 1);
if (nlvl > r) r = nlvl
}
return r
}, 0)
}
function flattenLvl(data, lvl, clvl = 1) {
return data.reduce((r, e) => {
if (Array.isArray(e)) {
const nested = flattenLvl(e, lvl, clvl + 1);
if (clvl == lvl) r.push(...nested);
else r.push(nested)
} else {
r.push(e)
}
return r;
}, [])
}
const lvl = lastLvl(array)
const result = flattenLvl(array, lvl)
console.log(result)