Commit cd55c53a by Jaxon

feat(treeData): 重构 normalizeTree 函数,优化参数处理和返回结构

1 parent e37adb1c
...@@ -21,31 +21,44 @@ ...@@ -21,31 +21,44 @@
export function normalizeTree( export function normalizeTree(
data, data,
{ {
labelKeys = ["label", "name", "title", "deptName", "itemName"], labelKeys,
valueKeys = ["value", "id", "deptId", "defineType"], valueKeys,
childrenKeys = ["children", "nodes", "childrenList", "itemList"], childrenKeys,
outputKeys,
} = {
labelKeys: ["label", "name", "title", "defineName", "itemName"],
valueKeys: ["value", "id", "deptId", "itemId"],
childrenKeys: ["children", "nodes", "childrenList", "itemList"],
outputKeys: {
label: "label",
value: "value",
children: "children",
},
}, },
) { ) {
if (!Array.isArray(data)) if (!Array.isArray(data))
return []; return [];
console.log("data", data);
return data.map((item) => { return data.map((item) => {
const label = labelKeys.find(k => item[k] != null); const label = labelKeys.find(k => item[k] != null);
const value = valueKeys.find(k => item[k] != null); const value = valueKeys.find(k => item[k] != null);
const childrenKey = childrenKeys.find(k => Array.isArray(item[k])); const childrenKey = childrenKeys.find(k => Array.isArray(item[k]));
return { const next = { ...item };
label: label ? item[label] : "", if (!(outputKeys.label in next))
value: value ? item[value] : "", next[outputKeys.label] = label ? item[label] : "";
children: childrenKey if (!(outputKeys.value in next))
? normalizeTree(item[childrenKey], { next[outputKeys.value] = value ? item[value] : "";
labelKeys, if (childrenKey) {
valueKeys, next[outputKeys.children] = normalizeTree(item[childrenKey], {
childrenKeys, labelKeys,
}) valueKeys,
: undefined, childrenKeys,
...(childrenKey ? undefined : { leaf: false }), outputKeys,
}; });
} else if (!("leaf" in next)) {
next.leaf = false;
}
return next;
}); });
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!