如何从普通数据结构构建树
How to build a tree from a plain data structure
我有以下带有 objects 的数组:
var arr= [{id: "1", state: "1", ndi: "1558.3045298", children: "null"},
{id: "2", state: "1", ndi: "1684.0732025", children: "null"},
{id: "3", state: "1", ndi: "1809.8418752", children: "null"},
{id: "4", state: "2", ndi: "1915.1603572", children: "null"},
{id: "5", state: "2", ndi: "2023.5463678", children: "null"}]
如何获得 object 树,其中 state
是 children 的根 object?
我猜您想要组织整个数组,以便项目是某些状态的子项。 运行 以下代码片段可获得 JSON 序列化结果以进行检查(单击显示代码片段以查看代码并 运行 它!):
var arr = [{
id: "1",
state: "1",
ndi: "1558.3045298",
children: "null"
}, {
id: "2",
state: "1",
ndi: "1684.0732025",
children: "null"
}, {
id: "3",
state: "1",
ndi: "1809.8418752",
children: "null"
}, {
id: "4",
state: "2",
ndi: "1915.1603572",
children: "null"
}, {
id: "5",
state: "2",
ndi: "2023.5463678",
children: "null"
}];
// This is going to hold states by id
var states = {};
// We iterate the array to get an organized by state children version
arr.forEach(function(item) {
// If state by id object hasn't the id yet we create an state object
// for current state id. There will be no duplicated states.
if (!states.hasOwnProperty(item.state)) {
states[item.state] = {
id: item.state,
children: []
};
}
// We add the item to the state by id object
states[item.state].children.push(item);
// This re-defines the state property to point to the
// item's parent state
item.state = states[item.state];
// We drop this property from the item because it's
// disturbing the new organized tree ;)
delete item.children;
});
var alreadySerialized = [];
// The second argument is required to avoid cyclic object
// serialization.
// Learn more here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
document.getElementById("result").textContent = JSON.stringify(states, function(key, val) {
if (val != null && typeof val == "object") {
if (alreadySerialized.indexOf(val) >= 0) {
return;
}
alreadySerialized.push(val);
}
return val;
});
<div id="result"></div>
这是转换后的示例结果数据:
{
"1":{
"id":"1",
"children":[
{
"id":"1",
"ndi":"1558.3045298"
},
{
"id":"2",
"ndi":"1684.0732025"
},
{
"id":"3",
"ndi":"1809.8418752"
}
]
},
"2":{
"id":"2",
"children":[
{
"id":"4",
"ndi":"1915.1603572"
},
{
"id":"5",
"ndi":"2023.5463678"
}
]
}
}
我有以下带有 objects 的数组:
var arr= [{id: "1", state: "1", ndi: "1558.3045298", children: "null"},
{id: "2", state: "1", ndi: "1684.0732025", children: "null"},
{id: "3", state: "1", ndi: "1809.8418752", children: "null"},
{id: "4", state: "2", ndi: "1915.1603572", children: "null"},
{id: "5", state: "2", ndi: "2023.5463678", children: "null"}]
如何获得 object 树,其中 state
是 children 的根 object?
我猜您想要组织整个数组,以便项目是某些状态的子项。 运行 以下代码片段可获得 JSON 序列化结果以进行检查(单击显示代码片段以查看代码并 运行 它!):
var arr = [{
id: "1",
state: "1",
ndi: "1558.3045298",
children: "null"
}, {
id: "2",
state: "1",
ndi: "1684.0732025",
children: "null"
}, {
id: "3",
state: "1",
ndi: "1809.8418752",
children: "null"
}, {
id: "4",
state: "2",
ndi: "1915.1603572",
children: "null"
}, {
id: "5",
state: "2",
ndi: "2023.5463678",
children: "null"
}];
// This is going to hold states by id
var states = {};
// We iterate the array to get an organized by state children version
arr.forEach(function(item) {
// If state by id object hasn't the id yet we create an state object
// for current state id. There will be no duplicated states.
if (!states.hasOwnProperty(item.state)) {
states[item.state] = {
id: item.state,
children: []
};
}
// We add the item to the state by id object
states[item.state].children.push(item);
// This re-defines the state property to point to the
// item's parent state
item.state = states[item.state];
// We drop this property from the item because it's
// disturbing the new organized tree ;)
delete item.children;
});
var alreadySerialized = [];
// The second argument is required to avoid cyclic object
// serialization.
// Learn more here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
document.getElementById("result").textContent = JSON.stringify(states, function(key, val) {
if (val != null && typeof val == "object") {
if (alreadySerialized.indexOf(val) >= 0) {
return;
}
alreadySerialized.push(val);
}
return val;
});
<div id="result"></div>
这是转换后的示例结果数据:
{
"1":{
"id":"1",
"children":[
{
"id":"1",
"ndi":"1558.3045298"
},
{
"id":"2",
"ndi":"1684.0732025"
},
{
"id":"3",
"ndi":"1809.8418752"
}
]
},
"2":{
"id":"2",
"children":[
{
"id":"4",
"ndi":"1915.1603572"
},
{
"id":"5",
"ndi":"2023.5463678"
}
]
}
}