gpt4 book ai didi

javascript - 通过javascript将json转换成树数据

转载 作者:行者123 更新时间:2023-11-30 13:44:19 24 4
gpt4 key购买 nike

我有这样的数据。

[
{
"dbname": "db1",
"tblname": "tbl1",
"colname": "ID_1",
"RowCounts": "50"
},
{
"dbname": "db1",
"tblname": "tbl1",
"colname": "ID_2",
"RowCounts": "50"
}, {
"dbname": "db1",
"tblname": "tbl1",
"colname": "ID_3",
"RowCounts": "50"
},
{
"dbname": "db1",
"tblname": "tbl1",
"colname": "ID_4",
"RowCounts": "30"
},{
"dbname": "db1",
"tblname": "tbl2",
"colname": "ID_1",
"RowCounts": "20"
},{
"dbname": "db1",
"tblname": "tbl2",
"colname": "ID_2",
"RowCounts": "30"
},{
"dbname": "db1",
"tblname": "tbl2",
"colname": "ID_3",
"RowCounts": "10"
},{
"dbname": "db1",
"tblname": "tbl2",
"colname": "ID_4",
"RowCounts": "60"
},{
"dbname": "db1",
"tblname": "tbl2",
"colname": "ID_5",
"RowCounts": "30"
},{
"dbname": "db1",
"tblname": "tbl3",
"colname": "ID_6",
"RowCounts": "20"
}
]

我想转换成这种格式。

["name":"db1", 
"rowcount":500, //sum of child row counts
"children":[
{"name":"tbl1",
"rowcount":200, //sum of children row counts
"children":[{
"name":"ID_1",
"rowcount":50
},{
"name":"ID_2",
"rowcount":50
}]},
{"name":"tbl2",
"rowcount":200,
"children":[{
}]}]}]

我该怎么做?我试过了,但没有用。

我导入了 csv 文件,但它的结构不适合树结构,所以我需要转换它。它有 3 个字段,dbname、tblname 和 colname 以及行数。只需将行数视为值(value)或其他东西。只是需要合并。

function createTreeData(structure) {
const node = (name, parent = null, row_cnt = 0) => ({
name,
parent,
row_cnt,
children: []
});
const addNode = (parent, child) => (parent.children.push(child), child);
const findNamed = (name, parent) => {
for (const child of parent.children) {
if (child.name === name) {
return child
}
const found = findNamed(name, child);
if (found) {
return found
}
}
}
const TOP_NAME = "Top",
top = node(TOP_NAME);
for (const children of structure) {
let par = top;
for (const name of children) {
const found = findNamed(name, par);
par = found ? found : addNode(par, node(name, par.name));
}
}
return top;
}

它创建树结构但不创建行数,并对其子项的行数求和。

如有任何帮助,我们将不胜感激。

最佳答案

您可以采用键数组作为嵌套结构,减少此结构并将新对象添加到最内部的数组中。在途中添加行数。

var data = [{ dbname: "db1", tblname: "tbl1", colname: "ID_1", RowCounts: "50" }, { dbname: "db1", tblname: "tbl1", colname: "ID_2", RowCounts: "50" }, { dbname: "db1", tblname: "tbl1", colname: "ID_3", RowCounts: "50" }, { dbname: "db1", tblname: "tbl1", colname: "ID_4", RowCounts: "30" }, { dbname: "db1", tblname: "tbl2", colname: "ID_1", RowCounts: "20" }, { dbname: "db1", tblname: "tbl2", colname: "ID_2", RowCounts: "30" }, { dbname: "db1", tblname: "tbl2", colname: "ID_3", RowCounts: "10" }, { dbname: "db1", tblname: "tbl2", colname: "ID_4", RowCounts: "60" }, { dbname: "db1", tblname: "tbl2", colname: "ID_5", RowCounts: "30" }, { dbname: "db1", tblname: "tbl3", colname: "ID_6", RowCounts: "20" }],
keys = ['dbname', 'tblname', 'colname'],
result = [];

data.forEach(o => keys.reduce((p, k) => {
var temp = (p.children = p.children || []).find(({ name }) => name === o[k]);
if (!temp) p.children.push(temp = { name: o[k], rowcount: 0 });
temp.rowcount += +o.RowCounts;
return temp;
}, { children: result }));

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

要仅在叶子上添加 rowcount,您可以采用旧方法并仅减少到最后一个嵌套级别,然后将具有计数的对象添加到最终数组。

您可以采用键数组作为嵌套结构,减少此结构并将新对象添加到最内部的数组中。在途中添加行数。

var data = [{ dbname: "db1", tblname: "tbl1", colname: "ID_1", RowCounts: "50" }, { dbname: "db1", tblname: "tbl1", colname: "ID_2", RowCounts: "50" }, { dbname: "db1", tblname: "tbl1", colname: "ID_3", RowCounts: "50" }, { dbname: "db1", tblname: "tbl1", colname: "ID_4", RowCounts: "30" }, { dbname: "db1", tblname: "tbl2", colname: "ID_1", RowCounts: "20" }, { dbname: "db1", tblname: "tbl2", colname: "ID_2", RowCounts: "30" }, { dbname: "db1", tblname: "tbl2", colname: "ID_3", RowCounts: "10" }, { dbname: "db1", tblname: "tbl2", colname: "ID_4", RowCounts: "60" }, { dbname: "db1", tblname: "tbl2", colname: "ID_5", RowCounts: "30" }, { dbname: "db1", tblname: "tbl3", colname: "ID_6", RowCounts: "20" }],
keys = ['dbname', 'tblname'],
result = data.reduce((r, o) => {
keys
.reduce((array, k) => {
var temp = array.find(({ name }) => name === o[k]);
if (!temp) array.push(temp = { name: o[k], children: [] });
return temp.children;
}, r)
.push({ name: o.colname, rowcount: +o.RowCounts });
return r;
}, []);

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

关于javascript - 通过javascript将json转换成树数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59593234/

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