[英]How to iterate over items in object/array, and build a nested JSON?
I'm working with some JSON data and I need to take data from one object, and build out a JSON object. 我正在处理一些JSON数据,我需要从一个对象中获取数据,并构建一个JSON对象。
EDITED: The resulting JSON is being used with the D3.js charting library, hence the strange question (and apparent down-votes) 编辑:生成的JSON与D3.js图表库一起使用,因此出现了一个奇怪的问题(显然是不赞成投票)
My data being returned from an API looks like this: 我从API返回的数据如下所示:
var data = [
[
{"id": 1, "key1":"value1", "key2":"value2", "key3":"value3"},
{"id": 2, "key1":"value1", "key2":"value2", "key3":"value3"},
{"id": 3, "key1":"value1", "key2":"value2", "key3":"value3"},
{"id": 4, "key1":"value1", "key2":"value2", "key3":"value3"},
{"id": 5, "key1":"value1", "key2":"value2", "key3":"value3"},
{"id": 6, "key1":"value1", "key2":"value2", "key3":"value3"}
]
];
and I need my JSON to have each object nested inside the previous like this: 我需要我的JSON将每个对象嵌套在上一个对象中,如下所示:
{
"name" : "1",
"children" : [
{
"name" : "2",
"children" : [
{
"name" : "3",
"children" : [
{
"name" : "4",
"children" : [
{
"name" : "5",
"children" : [
{
"name" : "6",
"children" : [ ]
}
]
}
]
}
]
}
]
}]
}
I've only stopped at 6 for demonstrative purposes - I'd need something that can work with N objects. 为了说明起见,我只停在6点-我需要可以与N个对象配合使用的东西。 Currently I'm looping over the
data
object with _.each
, but can't figure out how to do the nesting. 目前,我正在使用
_.each
遍历data
对象,但无法弄清楚如何进行嵌套。
Any help would be great - Thanks! 任何帮助都会很棒-谢谢!
var data = [ [{ "id": 1, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 2, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 3, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 4, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 5, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 6, "key1": "value1", "key2": "value2", "key3": "value3" }] ]; var r = (function rec(d, o, i) { if (i == -1) return o; var t = {}; t.name = d[i].id; t.children = []; t.children.push(o); return rec(d, t, --i); })(data[0], {}, data[0].length - 1); document.write('<pre>' + JSON.stringify(r, 0, 2) + '</pre>');
This is what I came up with. 这就是我想出的。 @isvforall was a little faster, but I like mine better.
@isvforall快了一点,但我更喜欢我的。 ;)
;)
var data = [ [{ "id": 1, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 2, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 3, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 4, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 5, "key1": "value1", "key2": "value2", "key3": "value3" }, { "id": 6, "key1": "value1", "key2": "value2", "key3": "value3" }] ]; var result = (function buildTree(input, index) { if (index >= input.length) return []; return [ { name: input[index].id, children: buildTree(input, index + 1) } ]; })(data[0], 0); document.write('<pre>' + JSON.stringify(result, 0, 2) + '</pre>');
Edit: Added code from @isvforall for demonstration. 编辑:从@isvforall添加了代码进行演示。 Our solutions both use recursion, we just build the tree differently.
我们的解决方案都使用递归,我们只是以不同的方式构建树。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.