gpt4 book ai didi

javascript - Json 数据到嵌套 Json 数据

转载 作者:行者123 更新时间:2023-12-03 05:31:26 26 4
gpt4 key购买 nike

我是 Jquery 新手,任何人都可以帮助我或告诉我如何处理这个问题, 我尝试了一些其他示例,但没有得到它们。

var arr = [
{ PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Update" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "View" },
{ PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Delete" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "Insert" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Update" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "View" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Insert" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Delete" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Update" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "View" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Insert" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Delete" }
],

我需要将此 json 数据转换为这样的嵌套数据,

[{
text: "Resource Management",
items: [{
text: "Manpower",
items: [{
text: "Update",
}, {
text: "Delete",
}, {
text: "Insert",
}, {
text: "View",
}]
}, {
text: "Vehicles",
items: [{
text: "Update",
}, {
text: "Delete",
}, {
text: "Insert",
}, {
text: "View",
}]
}]
}, {
text: "Contacts",
items: [{
text: "Manage Customers",
items: [{
text: "Update",
}, {
text: "Delete",
}, {
text: "Insert",
}, {
text: "View",
}]
}]
}]

提前致谢!请帮帮我。

最佳答案

纯js(有点原始,欢迎更好的解决方案):

var arr = [
{ PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Update" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "View" },
{ PARENTNODE: "Resource Management", CHILDNODE: "ManPower", OPERATIONNAME: "Delete" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Manpower", OPERATIONNAME: "Insert" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Update" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "View" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Insert" },
{ PARENTNODE: "Resource Management", CHILDNODE: "Vehicles", OPERATIONNAME: "Delete" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Update" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "View" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Insert" },
{ PARENTNODE: "Contacts", CHILDNODE: "Customers", OPERATIONNAME: "Delete" }
];

var output = [];

arr.forEach(function(item) {
var indexParentNode = addItem(item.PARENTNODE, output);
var indexChildNode = addItem(item.CHILDNODE, output[indexParentNode].items);
addItem(item.OPERATIONNAME, output[indexParentNode].items[indexChildNode].items, true);
});

function addItem(item, arr, withNoItems) {
var index = indexOf(item, arr);
var tpl = {text: item, items: []};

if (!! withNoItems) delete tpl.items;

if (index === -1) {
arr.push(tpl);
return arr.length - 1;
}
else {
return index;
}
}

function indexOf(needle, haystack) {
var i = 0;

while (i < haystack.length) {
if (haystack[i].text === needle) return i;
i++;
}

return -1;
}

console.log(output);

关于javascript - Json 数据到嵌套 Json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926790/

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