Array of JSON to Tree-structure JSON of Array
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
}]
}]
}
]
}
请帮我解决一个小问题。我认为我们不需要有关此的更多详细信息。
如果您想要其他任何内容,请随时对此发表评论 post。
编辑:
为了让问题更简单易懂。
编辑我的代码
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;
}
您可以采用嵌套循环方法,迭代 rawData
和 dimention
数组,同时为最后的 object 保存最后一项并减少其他给定的名称,直到最后的 children 找到数组。
在内部 lopp 中,查找同名的 children,如果没有找到,则生成并插入一个新数据集。
对 childrrem 使用外部检查有助于缩短没有 children 属性的路径。
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; }
我想编写一个函数,它可以将 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
}]
}]
}
]
}
请帮我解决一个小问题。我认为我们不需要有关此的更多详细信息。 如果您想要其他任何内容,请随时对此发表评论 post。
编辑: 为了让问题更简单易懂。
编辑我的代码
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;
}
您可以采用嵌套循环方法,迭代 rawData
和 dimention
数组,同时为最后的 object 保存最后一项并减少其他给定的名称,直到最后的 children 找到数组。
在内部 lopp 中,查找同名的 children,如果没有找到,则生成并插入一个新数据集。
对 childrrem 使用外部检查有助于缩短没有 children 属性的路径。
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; }