目前,我有一个嵌套的Javascript对象数组,其中包含许多具有相同名称但值不同的叶节点
{
"children": [
{
"name": "Central",
"children": [
{
"name": "Cellophane `Tape",
"value": 10.528979999999999
},
{
"name": "Cellophane Tape",
"value": 10.529
},
{
"name": "File Separator",
"value": 10.529
},
{
"name": "Hard Cover File",
"value": 10.529
},
{
"name": "Highlighter",
"value": 10.529
},
{
"name": "Office Chair",
"value": 10.529
},
{
"name": "Pencil",
"value": 10.529
},
{
"name": "Tape Dispenser",
"value": 10.529
},
{
"name": "File Cabinet",
"value": 21.058
},
{
"name": "Highlighter",
"value": 21.058
},
{
"name": "Office Chair",
"value": 21.058
},
{
"name": "Pencil",
"value": 21.058
},
{
"name": "Plastic Comb Binding",
"value": 21.058
},
{
"name": "Tape Dispenser",
"value": 21.058
},
{
"name": "White Board Markers",
"value": 21.058
},
{
"name": "File Separator",
"value": 23.273360000000004
},
{
"name": "Binder",
"value": 23.2734
},
{
"name": "Cellophane Tape",
"value": 23.2734
},
{
"name": "File Separator",
"value": 23.2734
},
{
"name": "Hard Cover File",
"value": 23.2734
},
{
"name": "Highlighter",
"value": 23.2734
},
{
"name": "Plastic Comb Binding",
"value": 23.2734
},
{
"name": "Tape Dispenser",
"value": 23.2734
},
{
"name": "White Board Markers",
"value": 23.2734
},
{
"name": "Binder",
"value": 64.0017
},
{
"name": "Eraser",
"value": 64.0017
},
{
"name": "File Separator",
"value": 64.0017
},
{
"name": "Office Chair",
"value": 64.0017
},
{
"name": "Plastic Comb Binding",
"value": 64.0017
},
{
"name": "Tape Dispenser",
"value": 64.0017
},
{
"name": "Binder",
"value": 64.00174
},
{
"name": "Tape Dispenser",
"value": 64.00174
},
{
"name": "Binder",
"value": 67.2899
},
{
"name": "Highlighter",
"value": 67.2899
},
{
"name": "Office Chair",
"value": 67.2899
},
{
"name": "Plastic Comb Binding",
"value": 67.2899
},
{
"name": "Tape Dispenser",
"value": 67.2899
},
{
"name": "Cellophane Tape",
"value": 74.2509
},
{
"name": "Office Chair",
"value": 74.2509
},
{
"name": "Pencil",
"value": 74.2509
},
{
"name": "Plastic Comb Binding",
"value": 74.2509
},
{
"name": "White Board Markers",
"value": 74.2509
},
{
"name": "Cellophane Tape",
"value": 79.7194
}
]
}
]
}
因此,我想做的是遍历此数组的所有叶节点,然后像这样将其聚合到每个父级,
const agregateDeep = x => {
if (Array.isArray(x.children)) {
x.children = x.children.map(deep);
return x;
} else {
return {
x: x.reduce(function (r, o) {
(r[o.name]) ? r[o.name] += o.value : r[o.name] = o.value;
return r;
})
};
}
};
类似的事情,但是使用reduce函数会返回一个平面数组。如果是这样,那么如何将这个平面数组重新插入到我的叶子节点中?
我能够检索所有叶节点并将其聚合,所以我的预期输出是这样的,但我希望它根据每个父节点来定。
我的数据:https://api.myjson.com/bins/gki6c
预期输出:
[
{
"name": "Central",
"children": [
{
{"name" : "Cellophane Tape","value":79.7194},
{"name" : "File Separator","value":64.0017},
{"name" : "Hard Cover File","value":23.2734},
{"name" : "Highlighter", "value":67.2899},
{"name" : "Office Chair","value":74.2509},
{"name" : "Pencil","value": 74.2509},
{"name" : "Tape Dispenser","value" : 67.2899},
{"name" : "File Cabinet","value": 21.058},
{"name" : "Plastic Comb Binding","value": 74.2509},
{"name" : "White Board Markers","value": 74.2509},
{"name" : "Binder","value" : 67.2899},
{"name" : "Eraser","value": 64.0017}
}
]
}
]
答案 0 :(得分:0)
hash
跨对象
var obj = {"children":[{"name":"Central","children":[{"name":"Cellophane `Tape","value":10.528979999999999},{"name":"Cellophane Tape","value":10.529},{"name":"File Separator","value":10.529},{"name":"Hard Cover File","value":10.529},{"name":"Highlighter","value":10.529},{"name":"Office Chair","value":10.529},{"name":"Pencil","value":10.529},{"name":"Tape Dispenser","value":10.529},{"name":"File Cabinet","value":21.058},{"name":"Highlighter","value":21.058},{"name":"Office Chair","value":21.058},{"name":"Pencil","value":21.058},{"name":"Plastic Comb Binding","value":21.058},{"name":"Tape Dispenser","value":21.058},{"name":"White Board Markers","value":21.058},{"name":"File Separator","value":23.273360000000004},{"name":"Binder","value":23.2734},{"name":"Cellophane Tape","value":23.2734},{"name":"File Separator","value":23.2734},{"name":"Hard Cover File","value":23.2734},{"name":"Highlighter","value":23.2734},{"name":"Plastic Comb Binding","value":23.2734},{"name":"Tape Dispenser","value":23.2734},{"name":"White Board Markers","value":23.2734},{"name":"Binder","value":64.0017},{"name":"Eraser","value":64.0017},{"name":"File Separator","value":64.0017},{"name":"Office Chair","value":64.0017},{"name":"Plastic Comb Binding","value":64.0017},{"name":"Tape Dispenser","value":64.0017},{"name":"Binder","value":64.00174},{"name":"Tape Dispenser","value":64.00174},{"name":"Binder","value":67.2899},{"name":"Highlighter","value":67.2899},{"name":"Office Chair","value":67.2899},{"name":"Plastic Comb Binding","value":67.2899},{"name":"Tape Dispenser","value":67.2899},{"name":"Cellophane Tape","value":74.2509},{"name":"Office Chair","value":74.2509},{"name":"Pencil","value":74.2509},{"name":"Plastic Comb Binding","value":74.2509},{"name":"White Board Markers","value":74.2509},{"name":"Cellophane Tape","value":79.7194}]}]};
function aggDeep(obj) {
let hasChildren = o => o.children && o.children.length,
hasNoChildren = o => !(o.children && o.children.length),
map = {},
aggDeepRec = ({
name,
children
}) => {
if (hasChildren({
children
})) {
let leafs = children.filter(hasNoChildren);
if (leafs.length) {
let entry = (map[name] = map[name] || {name, children: {}});
leafs.forEach(le => {
entry.children[le.name] = (entry.children[le.name] || 0) + le.value;
});
}
children.filter(hasChildren).forEach(c => aggDeepRec(c));
}
return map;
}
return Object.entries(aggDeepRec(obj)).reduce((res, [k, v])=>{
let {name} = v,
children = Object.entries(v.children).map(([name, value])=> ({name, value}));
return Object.assign(res, {[k]: {name, children}});
}, {});
}
var res = aggDeep(obj);
console.log(JSON.stringify(res, null, 4));