Commit cd55c53a by Jaxon

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

1 parent e37adb1c
......@@ -21,31 +21,44 @@
export function normalizeTree(
data,
{
labelKeys = ["label", "name", "title", "deptName", "itemName"],
valueKeys = ["value", "id", "deptId", "defineType"],
childrenKeys = ["children", "nodes", "childrenList", "itemList"],
labelKeys,
valueKeys,
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))
return [];
console.log("data", data);
return data.map((item) => {
const label = labelKeys.find(k => item[k] != null);
const value = valueKeys.find(k => item[k] != null);
const childrenKey = childrenKeys.find(k => Array.isArray(item[k]));
return {
label: label ? item[label] : "",
value: value ? item[value] : "",
children: childrenKey
? normalizeTree(item[childrenKey], {
labelKeys,
valueKeys,
childrenKeys,
})
: undefined,
...(childrenKey ? undefined : { leaf: false }),
};
const next = { ...item };
if (!(outputKeys.label in next))
next[outputKeys.label] = label ? item[label] : "";
if (!(outputKeys.value in next))
next[outputKeys.value] = value ? item[value] : "";
if (childrenKey) {
next[outputKeys.children] = normalizeTree(item[childrenKey], {
labelKeys,
valueKeys,
childrenKeys,
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!