gpt4 book ai didi

javascript - 从 MySQL/JSON 数据创建 JSON 层次树

转载 作者:行者123 更新时间:2023-12-03 00:01:13 25 4
gpt4 key购买 nike

我有一个非常大的 JSON 对象,我需要将其放入树中,但我不确定如何做到这一点。我正在使用 VueJs 和 Vuetify,它内置了 Treeview,但我不知道如何实际为树准备好数据。

这是我的数据... https://tpcrm.totalprocessing.com/api/get-channels

我需要的是这个(显然没有包含所有数据)

items: [
{
name: "Adboom",
id: "8ac9a4cb64b143910164b1b2ab0305db"
children: [
{
name: "Jaydox LTD",
id: "8ac9a4cb64b143910164b1b3009b05e2"
children: [
{
name: "beautifullyyoungskin.net"
id: "8ac9a4cb64b143910164b1b8004b063a"
},
{
name: "thinbodydiet.com"
id: "8ac9a4cb64b143910164b1b74af10630"
},
{
name: "youthfulskincare.net"
id: "8ac9a4cb64b143910164b1b77ba70634"
}
]
}
]
},
{
name: "Adult",
id: "8ac9a4c96489324601649354068034ab"
children: [
{
name: "Occonti Ltd",
id: "8ac9a4c965a8666d0165af279ca228dd"
children: [
{
name: "datinginthe.eu (3d Secure)"
id: "8ac9a4cd65a866700165b47a25c74e61"
},
{
name: "datinginthe.eu (Non-3d)"
id: "8ac9a4cd65a866700165b478f0574e48"
},
{
name: "datinginthe.eu - ST (Non-3d)"
id: "8ac9a4ca65a8a0670165d34366d53fc9"
},
{
name: "datinginthe.eu ST (3d Secure)"
id: "8ac9a4cb66aafd790166ad040a170b68"
}
]
}
]
}
]

最佳答案

通过对 nameid 使用相同的模式,您可以使用关键部分数组并通过它查找 id 和分组。

var data = [{ divisionName: "Adboom", divisionId: '000', merchantName: "Jaydox LTD", merchantId: '020', entityName: "beautifullyyoungskin.net", entityId: '500' }, { divisionName: "Adboom", divisionId: '000', merchantName: "Jaydox LTD", merchantId: '020', entityName: "thinbodydiet.com", entityId: '501' }, { divisionName: "Adboom", divisionId: '000', merchantName: "Jaydox LTD", merchantId: '020', entityName: "youthfulskincare.net", entityId: '502' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu (3d Secure)", entityId: '503' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu (Non-3d)", entityId: '504' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu - ST (Non-3d)", entityId: '505' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu ST (3d Secure)", entityId: '506' }],
keys = ["division", "merchant", "entity"],
result = data
.reduce((r, o) => {
keys.reduce((t, k) => {
var temp = (t.children = t.children || []).find(p => p.id === o[k + 'Id']);
if (!temp) {
t.children.push(temp = { name: o[k + 'Name'], id: o[k + 'Id'] });
}
return temp;
}, r);
return r;
}, {})
.children;

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 从 MySQL/JSON 数据创建 JSON 层次树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55170369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com