var rawData = [
{
"id": "1927544033741557762",
"identificationCode": "llx-dp",
"inventoryQuantity": "2",
"compId": "1925485944230080514",
"compTypeId": 3,
"compTypeName": "刀片",
"compTypeFlag": 3,
"compSpecificationsId": "1925442548148973570",
"compSpecifications": "DP25052201",
"compSpecificationsName": "DP25052201",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924350214015516673",
"supplierName": "山西刀具",
"stockInQuantity": "1",
"orderId": "1927544064158650369"
},
{
"id": "1927555896881704962",
"identificationCode": "TB2505280333",
"inventoryQuantity": "1",
"compId": "1927555395859509249",
"compTypeId": 3,
"compTypeName": "刀片",
"compTypeFlag": 3,
"compSpecificationsId": "1925442548148973570",
"compSpecifications": "DP25052201",
"compSpecificationsName": "DP25052201",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924349836968558594",
"supplierName": "恒丰五金",
"stockInQuantity": "0",
"orderId": "1927555973289340929"
},
{
"id": "1927555896881704963",
"identificationCode": "TB2505280334",
"inventoryQuantity": "1",
"compId": "1927555395876286466",
"compTypeId": 3,
"compTypeName": "刀片",
"compTypeFlag": 3,
"compSpecificationsId": "1925442548148973570",
"compSpecifications": "DP25052201",
"compSpecificationsName": "DP25052201",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924350156499025921",
"supplierName": "大连金星",
"stockInQuantity": "0",
"orderId": "1927555995313631234"
},
{
"id": "1928321045812948993",
"identificationCode": "DT100411new",
"inventoryQuantity": "1",
"compId": "1927691938603413506",
"compTypeId": 4,
"compTypeName": "刀头",
"compTypeFlag": 4,
"compSpecificationsId": "1924344981147234306",
"compSpecifications": "3/8\"",
"compSpecificationsName": "切削刀头",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924999855765712898",
"supplierName": "测试",
"stockInQuantity": "0",
"orderId": "1928321098346606594"
},
{
"id": "1928321045804560386",
"identificationCode": "HSK63A001",
"inventoryQuantity": "1",
"compId": "1924350556568518658",
"compTypeId": 2,
"compTypeName": "刀柄",
"compTypeFlag": 2,
"compSpecificationsId": "1924344837609762817",
"compSpecifications": "HSK63A",
"compSpecificationsName": "HSK刀柄",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924350214015516673",
"supplierName": "山西刀具",
"stockInQuantity": "0",
"orderId": "1928321098346606594"
},
{
"id": "1928321045812948993",
"identificationCode": "DT100411new",
"inventoryQuantity": "1",
"compId": "1927691938603413506",
"compTypeId": 4,
"compTypeName": "刀头",
"compTypeFlag": 4,
"compSpecificationsId": "1924344981147234306",
"compSpecifications": "3/8\"",
"compSpecificationsName": "切削刀头",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924999855765712898",
"supplierName": "测试",
"stockInQuantity": "0",
"orderId": "1928321101563637762"
},
{
"id": "1928321045804560386",
"identificationCode": "HSK63A001",
"inventoryQuantity": "1",
"compId": "1924350556568518658",
"compTypeId": 2,
"compTypeName": "刀柄",
"compTypeFlag": 2,
"compSpecificationsId": "1924344837609762817",
"compSpecifications": "HSK63A",
"compSpecificationsName": "HSK刀柄",
"identificationMethod": 2,
"remark": null,
"type": 1,
"supplierId": "1924350214015516673",
"supplierName": "山西刀具",
"stockInQuantity": "0",
"orderId": "1928321101563637762"
}
]
function buildTree(data) {
const createNode = () => ({
name: '',
children: [],
data: [],
});
const root = createNode();
root.name = 'Root';
data.forEach((item) => {
const { compTypeName, compSpecifications, type, id, supplierName } = item;
let categoryNode = findOrCreateChild(root, compTypeName);
let specNode = findOrCreateChild(categoryNode, compSpecifications);
const nodeKey = type === 1 ? '厂内' : supplierName;
let leafNode = findOrCreateChild(specNode, nodeKey);
if (leafNode.children.length === 0) {
leafNode.data.push(id);
}
});
const cleanTree = (node) => {
return {
name: node.name,
data: node.children.length === 0 ? node.data.join(',') : '',
children: node.children.map(cleanTree),
};
};
return cleanTree(root);
}
function findOrCreateChild(parent, name) {
let child = parent.children.find((c) => c.name === name);
if (!child) {
child = {
name,
children: [],
data: [],
};
parent.children.push(child);
}
return child;
}
const treeData = buildTree(rawData);
console.log(JSON.stringify(treeData, null, 2));