如何迭代对象/数组中的项,并构建嵌套的JSON?

时间:2016-04-07 13:19:54

标签: javascript json d3.js underscore.js

我正在使用一些JSON数据,我需要从一个对象获取数据,并构建一个JSON对象。

EDITED:生成的JSON正在与D3.js图表​​库一起使用,因此这个奇怪的问题(以及明显的向下投票)

我从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"}
    ]
];

我需要我的JSON让每个对象嵌套在上面的内容中,如下所示:

{
    "name" : "1",
    "children" : [
    {
        "name" : "2",
        "children" : [
            {
                "name" : "3",
                "children" : [
                    {
                        "name" : "4",
                        "children" : [
                            {
                                "name" : "5",
                                "children" : [
                                    {
                                        "name" : "6",
                                        "children" : [ ]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }]
}

为了示范目的,我只停在6点 - 我需要一些可以使用N个对象的东西。 目前我正在使用data循环_.each对象,但无法弄清楚如何进行嵌套。

任何帮助都会很棒 - 谢谢!

2 个答案:

答案 0 :(得分:2)



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>');
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这就是我想出的。 @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>');

编辑:添加了@isvforall的代码进行演示。我们的解决方案都使用递归,我们只是以不同的方式构建树。