繁体   English   中英

JSON 数组到树状结构 JSON 数组

[英]Array of JSON to Tree-structure JSON of Array

我想编写一个函数,它可以将一个 JSON 数组转换为数组的树状结构 JSON。

我有一个像这样的 JSON 数组:

    var rawData = [{
    "dimension": ["a", "c", "f"],
    "metric": [26]
}, {
    "dimension": ["a", "b", "e"],
    "metric": [12]
}, {
    "dimension": ["a", "d", "e"],
    "metric": [7]
}, {
    "dimension": ["a", "b", "f"],
    "metric": [5]
}, {
    "dimension": ["a", "c", "e"],
    "metric": [2]
}, {
    "dimension": ["a", "d", "f"],
    "metric": [1]
}, {
    "dimension": ["a", "k", ""],
    "metric": [2]
},{
    "dimension": ["b", "c", "d"],
    "metric": [2]
}];

我期待这样的输出:

output:
{
    name: 'start',
    children: [{
            name: 'a',
            children: [{
                    name: 'c',
                    children: [{
                        name: 'f',
                        value: 26
                    }, {
                        name: 'e',
                        value: 2
                    }]
                },
                {
                    name: 'b',
                    children: [{
                        name: 'e',
                        value: 12
                    }, {
                        name: 'f',
                        value: 5
                    }]
                },
                {
                    name: 'd',
                    children: [{
                        name: 'e',
                        value: 7
                    }, {
                        name: 'f',
                        value: 1
                    }]
                },
                {
                    name: 'k',
                    value: 2
                }
            ]
        },
        {
            name: 'b',
            children: [{
                name: 'c',
                children: [{
                    name: 'd',
                    value: 2
                }]
            }]
        }
    ]
}

请帮我做一个小查询。 我不认为我们需要关于此的更多细节。 如果您想要其他任何内容,请随时对此帖子发表评论。

编辑:使问题更容易理解。

编辑我的代码

var output = {
    name: "start",
    children: []
};
var len = rawData.length;
for (var i = 0; i < len; i++) {
    rawChild = rawData[i];
    cat = createJson({}, rawChild.dimension.filter(n => n), rawChild.metric[0]);
    if (i == 0)
        output.children.push(cat);
    else {
        mergeData(output, output.children, cat);
    }
}


function mergeData(parent, child, cat) {
    if (child) {
        for (var index = 0; index < child.length; index++) {
            var element = child[index];

            if (cat.children) {
                if (element.name == cat.name) {
                    parent = mergeData(element, element.children, cat.children[0]);
                    return parent;
                } else {
                    continue;
                }
            } else {
                if (element.name == cat.name) {
                    parent = mergeData(element, element.children, cat);
                    return parent;
                } else {
                    continue;
                }
            }

        }
        parent.children.push(cat);
        return parent;
    } else {
        return;
    }
}
console.log(util.inspect(output, false, null, true));

function createJson(mainObj, names, value) {
    if (!Array.isArray(names)) {
        mainObj.name = names;
        mainObj.value = value;
        return mainObj;
    } else {
        for (var index = 0; index < names.length; index++) {
            if (index == names.length - 1) {
                mainObj = createJson(mainObj, names[index], value);
            } else {
                mainObj.name = names[index];
                newarr = names;
                newarr.shift();
                mainObj.children = [createJson({}, newarr, value)];
            }
        }
    }
    return mainObj;
}

您可以通过迭代rawDatadimention数组来采用嵌套循环方法,同时保存最终对象的最后一项并减少其他给定名称,直到找到最终的子数组。

在内部 lopp 中,查找同名的孩子,如果未找到,则生成并插入新数据集。

对 childrrem 使用外部检查有助于缩短没有子属性的路径。

 var rawData = [{ dimension: ["a", "c", "f"], metric: [26] }, { dimension: ["a", "b", "e"], metric: [12] }, { dimension: ["a", "d", "e"], metric: [7] }, { dimension: ["a", "b", "f"], metric: [5] }, { dimension: ["a", "c", "e"], metric: [2] }, { dimension: ["a", "d", "f"], metric: [1] }, { dimension: ["a", "k", ""], metric: [2] }, { dimension: ["b", "c", "d"], metric: [2] }], result = { name: "start", children: [] }; rawData.forEach(({ dimension: path, metric: [value] }) => { while (!path[path.length - 1]) path.pop(); // remove falsy values from end var name = path.pop(); path .reduce((result, name) => { var temp = result.find(o => o.name === name); if (!temp) { result.push(temp = { name }); } temp.children = temp.children || []; return temp.children; }, result.children) .push({ name, value }); }); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM