/** * 用于动态表结构的 tableProps 生成 * @param {*} nameData * @returns */ export default function handleNameData(nameData) { const props = step1(nameData.filter((item) => item.tree == 1)); step2( props, nameData.filter((item) => item.tree == 2) ); // console.log('level 1', JSON.stringify(props, null, 2)); return props; } function step1(tree1) { return Array.from(new Set(tree1.map((item) => item.name))) .sort() .map((item) => ({ prop: item, label: item, align: 'center', children: [], })); } function step2(firstTierProps, tree2) { tree2.map((nd) => { const parent = firstTierProps.find( ({ prop }) => nd.parentId.indexOf(prop) > -1 ); if (notRepeat(parent.children, nd.name)) { parent.children.push({ label: nd.name, prop: `${parent.prop}-${nd.name}`, align: 'center', }); } }); } function notRepeat(propArray, name) { return propArray.every((item) => item.label !== name); }