Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON数组到JSON数组的树结构_Javascript_Arrays_Json_Treemap - Fatal编程技术网

Javascript JSON数组到JSON数组的树结构

Javascript JSON数组到JSON数组的树结构,javascript,arrays,json,treemap,Javascript,Arrays,Json,Treemap,我想写一个函数,它可以把一个JSON数组转换成JSON数组的树结构 我有一个JSON数组,如下所示: var rawData = [{ "dimension": ["a", "c", "f"], "metric": [26] }, { "dimension": ["a", "b", "e"], "metric": [12] }, { "dimension": ["a", "d", "e"], "metric": [7] }, { "

我想写一个函数,它可以把一个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 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;
}
var输出={
名称:“开始”,
儿童:[]
};
var len=rawData.length;
对于(变量i=0;in),rawChild.metric[0]);
如果(i==0)
输出.儿童.推送(cat);
否则{
合并数据(输出、输出子项、cat);
}
}
函数合并数据(父、子、类别){
如果(儿童){
对于(变量索引=0;索引
您可以采用嵌套循环方法,通过迭代
rawData
dimention
数组,同时为最终对象保存最后一项,并减少其他给定名称,直到找到最终子数组

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

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

var rawData=[{dimension:[“a”、“c”、“f”]、度量:[26]}、{dimension:[“a”、“b”、“e”]、度量:[12]}、{dimension:[“a”、“d”、“e”]、度量:[7]}、{dimension:[“a”、“b”、“f”]、度量:[5]}、{dimension:[“a”、“c”、“e”]、度量:[2]}、{dimension:[“a”、“d”、“f”]、度量:[1]}、{dimension:[“a”、“k”、”k”、“,”,{维度:[“b”、“c”、“d”],度量:[2]}],
结果={name:“start”,子项:[]};
forEach({dimension:path,metric:[value]})=>{
而(!path[path.length-1])path.pop();//从末尾删除falsy值
var name=path.pop();
路径
.reduce((结果、名称)=>{
var temp=result.find(o=>o.name==name);
如果(!temp){
push(temp={name});
}
临时儿童=临时儿童| |[];
返回临时儿童;
},结果(儿童)
.push({name,value});
});
console.log(结果);

.as控制台包装{最大高度:100%!重要;顶部:0;}
您尝试了什么?您获得输出的算法是什么?我在帖子中添加代码是为了更容易理解。我想知道一种更优化的方法。您能用文字发布预期的输出以创建反向工程吗?您的代码有点混乱。