我有一个JavaScript对象,其中包含许多属性。我需要创建另一个类似的层次结构,但我只需要很少的属性。
[
{
_id: "559d0fcdd791e5c452345e80",
name: "Books",
lastUpdated: "2015-07-08T11:55:57.891Z",
body: "",
children: [
{
_id: "559d0fd9d791e5c452345e81",
name: "web apps",
__v: 2,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:09.356Z",
body: "",
children: [
{
_id: "559d0fe9d791e5c452345e83",
name: ".net",
__v: 1,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:25.139Z",
body: "",
children: [
{
_id: "559d0ff2d791e5c452345e84",
name: "asp.net",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:34.306Z",
body: "",
children: [
],
parent: "559d0fe9d791e5c452345e83",
description: ""
}
],
parent: "559d0fd9d791e5c452345e81",
description: ""
},
{
_id: "559d0ffad791e5c452345e85",
name: "java",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:42.663Z",
body: "",
children: [
],
parent: "559d0fd9d791e5c452345e81",
description: ""
}
],
parent: "559d0fcdd791e5c452345e80",
description: ""
},
{
_id: "559d0fe0d791e5c452345e82",
name: "db",
__v: 2,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:16.436Z",
body: "",
children: [
{
_id: "559d1006d791e5c452345e86",
name: "SQL server",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:54.289Z",
body: "",
children: [
],
parent: "559d0fe0d791e5c452345e82",
description: ""
},
{
_id: "559d101ad791e5c452345e87",
name: "NO SQL",
__v: 1,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:57:14.988Z",
body: "",
children: [
{
_id: "559d1025d791e5c452345e88",
name: "MongoDB",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:57:25.044Z",
body: "",
children: [
],
parent: "559d101ad791e5c452345e87",
description: ""
}
],
parent: "559d0fe0d791e5c452345e82",
description: ""
}
],
parent: "559d0fcdd791e5c452345e80",
description: ""
}
],
parent: null,
description: "Some books"
}
]
由此我需要创建一个这样的层次结构:
[{
text: 'books',
nodes: [{
text:'web apps',
nodes:[{
text: '.net',
nodes:[{
text:'asp.net',
nodes:[]
}]
},{
text: 'java',
nodes:[]
}]
},{
text: 'DB',
nodes: [{
text: 'SQL Server'
},{
text: 'NO SQL',
nodes:[{
text: 'MongoDB'
}]
}]
}]
}]
我正在尝试创建一个递归循环,但不确定如何添加子节点。我怎样才能得到理想的结果。
答案 0 :(得分:1)
你需要一个递归函数。这个是为您的特定数据输入而构建的:
function reduce (input, output) {
output = [];
$.each(input, function(key, val) {
output[key] = {};
output[key].text = val.name;
output[key].nodes = reduce(val.children);
});
return output;
}
并使用output = reduce(input);
这是一个片段:
input = [
{
_id: "559d0fcdd791e5c452345e80",
name: "Books",
lastUpdated: "2015-07-08T11:55:57.891Z",
body: "",
children: [
{
_id: "559d0fd9d791e5c452345e81",
name: "web apps",
__v: 2,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:09.356Z",
body: "",
children: [
{
_id: "559d0fe9d791e5c452345e83",
name: ".net",
__v: 1,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:25.139Z",
body: "",
children: [
{
_id: "559d0ff2d791e5c452345e84",
name: "asp.net",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:34.306Z",
body: "",
children: [
],
parent: "559d0fe9d791e5c452345e83",
description: ""
}
],
parent: "559d0fd9d791e5c452345e81",
description: ""
},
{
_id: "559d0ffad791e5c452345e85",
name: "java",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:42.663Z",
body: "",
children: [
],
parent: "559d0fd9d791e5c452345e81",
description: ""
}
],
parent: "559d0fcdd791e5c452345e80",
description: ""
},
{
_id: "559d0fe0d791e5c452345e82",
name: "db",
__v: 2,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:16.436Z",
body: "",
children: [
{
_id: "559d1006d791e5c452345e86",
name: "SQL server",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:56:54.289Z",
body: "",
children: [
],
parent: "559d0fe0d791e5c452345e82",
description: ""
},
{
_id: "559d101ad791e5c452345e87",
name: "NO SQL",
__v: 1,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:57:14.988Z",
body: "",
children: [
{
_id: "559d1025d791e5c452345e88",
name: "MongoDB",
__v: 0,
media: {
images: [
]
},
lastUpdated: "2015-07-08T11:57:25.044Z",
body: "",
children: [
],
parent: "559d101ad791e5c452345e87",
description: ""
}
],
parent: "559d0fe0d791e5c452345e82",
description: ""
}
],
parent: "559d0fcdd791e5c452345e80",
description: ""
}
],
parent: null,
description: "Some books"
}
]
function reduce (input, output) {
output = [];
$.each(input, function(key, val) {
output[key] = {};
output[key].text = val.name;
output[key].nodes = reduce(val.children);
});
return output;
}
output = reduce(input);
$('div').text(JSON.stringify(output));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div></div>