簡體   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