如何将对象转换为数组



我有一个对象数组:

arrayOfObjects = [
{
"name": "A",
"child": [
{
"name": "A1",
"child": [
{
"name": "A1a"
},
{
"name": "A1b"
},
{
"name": "A1c"
}
]
},
{
"name": "A2",
"child": [
{
"name": "A2a"
},
{
"name": "A2b"
}
]
},
{
"name": "A3",
"child": [
{
"name": "A3a"
},
{
"name": "A3b"
},
{
"name": "A3c"
},
{
"name": "A3d"
}
]
},
{
"name": "A4",
"child": [
{
"name": "A4a"
},
{
"name": "A4b"
},
{
"name": "A4c"
}
]
}
]
},
{
"name": "B",
"child": [
{
"name": "B1",
"child": [
{
"name": "B1a"
},
{
"name": "B1b"
}
]
},
{
"name": "B2",
"child": [
{
"name": "B2a"
},
{
"name": "B2b"
}
]
},
{
"name": "B3",
"child": [
{
"name": "B3A"
},
{
"name": "B3b"
},
{
"name": "B3c"
},
{
"name": "B3d"
},
{
"name": "B3e"
}
]
}
]
},
{others objects with same structure}
]

我需要从这个对象数组中创建一个数组的数组,结构如下:

array = [[A,A1,A1a],
[A1b],
[A1c],
[A2,A2a],
[A2b],
[A3,A3a],
[A3b],
[A3c],
[A3d],
[A4,A4a],
[A4b],
[A4c],
[B,B1,B1a] 
and so on]

目的是为Google电子表格中的每个数组写一行。

对于forEach方法,我有点迷失了,在每个对象中,子键包含其他有子对象的数组,我不知道如何在这个层次结构中迭代

您可以使用自包含递归(不依赖于全局累加器或传递的上下文)来实现这一点,方法是在返回的过程中将最深层递归调用的结果分布到每个级别。这里对递归调用的结果使用嵌套的for...of循环。

const arrayOfObjects = [{ name: 'A', child: [{ name: 'A1', child: [{ name: 'A1a' }, { name: 'A1b' }, { name: 'A1c' }], }, { name: 'A2', child: [{ name: 'A2a' }, { name: 'A2b' }] }, { name: 'A3', child: [{ name: 'A3a' }, { name: 'A3b' }, { name: 'A3c' }, { name: 'A3d' },], }, { name: 'A4', child: [{ name: 'A4a' }, { name: 'A4b' }, { name: 'A4c' }], },], }, { name: 'B', child: [{ name: 'B1', child: [{ name: 'B1a' }, { name: 'B1b' }] }, { name: 'B2', child: [{ name: 'B2a' }, { name: 'B2b' }] }, { name: 'B3', child: [{ name: 'B3A' }, { name: 'B3b' }, { name: 'B3c' }, { name: 'B3d' }, { name: 'B3e' },], },], },];
const condenseArray = (arr) => {
const res = [];
let lvl = [];
for (const { name, child } of arr) {
lvl.push(name);

if (child?.length) {
for (const n of condenseArray(child)) {
lvl.push(...n);
res.push(lvl);
lvl = [];
}
} else {
res.push(lvl);
lvl = [];
}
}
return res;
};
const result = condenseArray(arrayOfObjects);
for (const arr of result) {
console.log(`[ ${arr.map((n) => `'${n}'`).join(', ')} ]`.padStart(20, ' '));
}
/* Output
[
['A', 'A1', 'A1a'],
['A1b'],
['A1c'],
['A2', 'A2a'],
['A2b'],
...
['B', 'B1', 'B1a'],
['B1b'],
['B2', 'B2a'],
...
]
*/
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

但是简单地为每个子节点创建完整的行在逻辑和输出中都更清晰。

const arrayOfObjects = [{ name: 'A', child: [{ name: 'A1', child: [{ name: 'A1a' }, { name: 'A1b' }, { name: 'A1c' }], }, { name: 'A2', child: [{ name: 'A2a' }, { name: 'A2b' }] }, { name: 'A3', child: [{ name: 'A3a' }, { name: 'A3b' }, { name: 'A3c' }, { name: 'A3d' },], }, { name: 'A4', child: [{ name: 'A4a' }, { name: 'A4b' }, { name: 'A4c' }], },], }, { name: 'B', child: [{ name: 'B1', child: [{ name: 'B1a' }, { name: 'B1b' }] }, { name: 'B2', child: [{ name: 'B2a' }, { name: 'B2b' }] }, { name: 'B3', child: [{ name: 'B3A' }, { name: 'B3b' }, { name: 'B3c' }, { name: 'B3d' }, { name: 'B3e' },], },], },];

const condenseArray = (arr) => {
const res = [];
for (const { name, child } of arr) {
let lvl = [name];
if (child?.length) {
for (const n of condenseArray(child)) {
res.push([...lvl, ...n]);
}
} else {
res.push(lvl);
}
}
return res;
};
const result = condenseArray(arrayOfObjects);
for (const arr of result) {
console.log(`[ ${arr.map((n) => `'${n}'`).join(', ')} ]`.padStart(20, ' '));
}
/* Output
[
['A', 'A1', 'A1a'],
['A', 'A1', 'A1b'],
['A', 'A1', 'A1c'],
['A', 'A2', 'A2a'],
['A', 'A2', 'A2b'],
...
]
*/
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

我已经做过了…

const arrayOfObjects = 
[ { name: 'A', child: 
[ { name: 'A1', child: 
[ { name: 'A1a' }, { name: 'A1b' }, { name: 'A1c' } 
] } 
, { name: 'A2', child: 
[ { name: 'A2a' }, { name: 'A2b' } 
] } 
, { name: 'A3', child: 
[ { name: 'A3a' }, { name: 'A3b' }, { name: 'A3c' }, { name: 'A3d' } 
] } 
, { name: 'A4', child: 
[ { name: 'A4a' }, { name: 'A4b' }, { name: 'A4c' } 
] } ] } 
, { name: 'B', child: 
[ { name: 'B1', child: 
[ { name: 'B1a' }, { name: 'B1b' } 
] } 
, { name: 'B2', child: 
[ { name: 'B2a' }, { name: 'B2b' } 
] } 
, { name: 'B3', child: 
[ { name: 'B3A' }, { name: 'B3b' }, { name: 'B3c' }, { name: 'B3d' }, { name: 'B3e' } 
] } ] } ];

const res = [], elm = [];
const AddElm = arr =>
{
arr.forEach(({name,child}) =>
{
elm.push(name)
if (!!child)
AddElm(child);
else 
{
res.push([...elm]);
elm.length = 0; 
}
});
}
AddElm(arrayOfObjects);
console.log( res );
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

合成结果

[ [ 'A', 'A1', 'A1a' ] 
, [            'A1b' ] 
, [            'A1c' ] 
, [      'A2', 'A2a' ] 
, [            'A2b' ] 
, [      'A3', 'A3a' ] 
, [            'A3b' ] 
, [            'A3c' ] 
, [            'A3d' ] 
, [      'A4', 'A4a' ] 
, [            'A4b' ] 
, [            'A4c' ] 
, [ 'B', 'B1', 'B1a' ] 
, [            'B1b' ] 
, [      'B2', 'B2a' ] 
, [            'B2b' ] 
, [      'B3', 'B3A' ] 
, [            'B3b' ] 
, [            'B3c' ] 
, [            'B3d' ] 
, [            'B3e' ] 
]  

相关内容

  • 没有找到相关文章

最新更新