如何加入每个级别具有不同分隔符的 n 维数组?
How to join an n-dimension array with different separators for each level?
示例输入
数组
这里我展示了一个 3 维数组,但实际维数不同,被称为 n
。
[
[
[1,2],
[3,4]
],
[
[5,6],
[7,8]
]
]
分隔符
它的长度与数组的维数相同(n
)其中第i个元素代表数组第i层的分隔符。
[',', '_', '-']
期望的输出
1-2_3-4,5-6_7-8
我试过的
它适用于 3 维数组,但不适用于 4 维数组。
我知道我的代码出了什么问题,但我不知道如何修复它。
此外,我认为还有更简单and/or更有效的方法。
三维(工作)
const array = [[[1,2],[3,4]],[[5,6],[7,8]]];
const separators = [',', '_', '-'];
const _separators = separators.reverse();
let i;
function join(array, first = false) {
const next = Array.isArray(array[0]);
let result;
if (next) {
result = array.map(e => {
if (first) { i = 0; }
return join(e);
});
i++;
result = result.join(_separators[i]);
}
else {
result = array.join(_separators[i]);
}
return result;
}
const result = join(array, true);
console.log(result);
4 维(无法正常工作)
const array = [[[[1,2],[3,4]],[[5,6],[7,8]]],[[['A','B'],['C','D']],[['E','F'],['G','H']]]];
const separators = ['|', ',', '_', '-'];
const _separators = separators.reverse();
let i;
function join(array, first = false) {
const next = Array.isArray(array[0]);
let result;
if (next) {
result = array.map(e => {
if (first) { i = 0; }
return join(e);
});
i++;
result = result.join(_separators[i]);
}
else {
result = array.join(_separators[i]);
}
return result;
}
const result = join(array, true);
console.log(result);
// desired output: 1-2_3-4,5-6_7-8|A-B_C-D,E-F_G-H
类似这样的递归
const join = (array, separators, depth) => {
if (depth < separators.length -1) {
return array.map(el => join(el, separators, depth + 1)).join(separators[depth]);
} else {
return array.join(separators[depth]);
}
};
{
const array = [[[1,2],[3,4]],[[5,6],[7,8]]];
const separators = [',', '_', '-'];
console.log(join(array, separators, 0));
}
{
const array = [[[[1,2],[3,4]],[[5,6],[7,8]]],[[['A','B'],['C','D']],[['E','F'],['G','H']]]];
const separators = ['|', ',', '_', '-'];
console.log(join(array, separators, 0));
}
示例输入
数组
这里我展示了一个 3 维数组,但实际维数不同,被称为 n
。
[
[
[1,2],
[3,4]
],
[
[5,6],
[7,8]
]
]
分隔符
它的长度与数组的维数相同(n
)其中第i个元素代表数组第i层的分隔符。
[',', '_', '-']
期望的输出
1-2_3-4,5-6_7-8
我试过的
它适用于 3 维数组,但不适用于 4 维数组。
我知道我的代码出了什么问题,但我不知道如何修复它。
此外,我认为还有更简单and/or更有效的方法。
三维(工作)
const array = [[[1,2],[3,4]],[[5,6],[7,8]]];
const separators = [',', '_', '-'];
const _separators = separators.reverse();
let i;
function join(array, first = false) {
const next = Array.isArray(array[0]);
let result;
if (next) {
result = array.map(e => {
if (first) { i = 0; }
return join(e);
});
i++;
result = result.join(_separators[i]);
}
else {
result = array.join(_separators[i]);
}
return result;
}
const result = join(array, true);
console.log(result);
4 维(无法正常工作)
const array = [[[[1,2],[3,4]],[[5,6],[7,8]]],[[['A','B'],['C','D']],[['E','F'],['G','H']]]];
const separators = ['|', ',', '_', '-'];
const _separators = separators.reverse();
let i;
function join(array, first = false) {
const next = Array.isArray(array[0]);
let result;
if (next) {
result = array.map(e => {
if (first) { i = 0; }
return join(e);
});
i++;
result = result.join(_separators[i]);
}
else {
result = array.join(_separators[i]);
}
return result;
}
const result = join(array, true);
console.log(result);
// desired output: 1-2_3-4,5-6_7-8|A-B_C-D,E-F_G-H
类似这样的递归
const join = (array, separators, depth) => {
if (depth < separators.length -1) {
return array.map(el => join(el, separators, depth + 1)).join(separators[depth]);
} else {
return array.join(separators[depth]);
}
};
{
const array = [[[1,2],[3,4]],[[5,6],[7,8]]];
const separators = [',', '_', '-'];
console.log(join(array, separators, 0));
}
{
const array = [[[[1,2],[3,4]],[[5,6],[7,8]]],[[['A','B'],['C','D']],[['E','F'],['G','H']]]];
const separators = ['|', ',', '_', '-'];
console.log(join(array, separators, 0));
}