1 回答
TA贡献1830条经验 获得超9个赞
您可以使用转换后的部分映射新对象。
const convert = o => {
var children;
if ('group' in o) o = o.group;
if ('children' in o) children = o.children.map(convert);
return Object.assign({}, o, { parent: o.parent.length ? { id: o.parent[0].parent } : null }, children && { children });
};
var data = [{ id: 1, name: "dad", parent: [], children: [{ group: { id: 2, name: "child1", parent: [{ parent: 1 }] } }, { group: { id: 3, name: "child2", parent: [{ parent: 1 }] } }, { group: { id: 8, name: "child3", parent: [{ parent: 1 }] } }] }],
result = data.map(convert);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
添加回答
举报
