nodeObj: {
childNode: {
name: "张三",
age: 11,
childNode: {
name: "李四",
age: 13,
},
},
},
nodeArr: [
{
name: "张三",
age: 11,
},
{
name: "李四",
age: 13,
},
],
1.第一种方法
nestTree(arr, curIndex = 0) {
return {
...arr[curIndex],
childNode: curIndex < arr.length - 1 ? nestTree(arr, curIndex + 1) : {},
};
},
2.第二种方法
const nodeObj = nodeArr.reduceRight((pre, cur) => {
({ ...cur, childNode: pre });
}, null);
js对象数组转嵌套对象
最新推荐文章于 2023-11-01 09:42:08 发布