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



我想写一个函数,它可以将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; }

最新更新