遍历 JSON 个对象并通过独特的深层属性对值求和
Loop through JSON object and sum values by unique deep properties
下面是我的 JSON 对象(下面只是我感兴趣的值的例子,实际上除了成本还有很多其他参数)
我正在尝试在 experss js 中使用下划线
[
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
我想要如下输出
Joe 950
Tam 500
我试过这个:
var iw={};
iw = Object.keys(myJsonObject.reduce((iw, curr) => {
//iw[curr.author.id] = iw[curr.author.id]
iw[curr.author.id].cost += parseInt(curr.cost);
return iw;
}, iw)).map(key => iw[key]);
console.log("New ::"+iw);
但我没有得到我所希望的:
TypeError: Cannot read property 'cost' of undefined
at Object.keys.myJsonObject.reduce (repl:3:7)
at Array.reduce (<anonymous>)
New ::[object Object]
这可能是一种更简洁的方法,但是一个简单的 forEach 就可以了。显然这里输出的是一个对象,里面包含了想要的结果。
const data = [
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
let a = {};
data.forEach(e => a[e.author.name] ? a[e.author.name] += e.Cost : a[e.author.name] = e.Cost);
console.log(a);
如Fraser所述
您的原始代码存在问题,无法检查该用户是否已经存在于 iw 对象中
您更正的解决方案:
myJsonObject =
[
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
var iw={};
iw = myJsonObject.reduce((iw, curr) => {
iw[curr.author.name] ? iw[curr.author.name] += curr.Cost : iw[curr.author.name] = curr.Cost;
return iw;
}, iw);
console.log(iw);
结果:
{Joe: 950, Tam: 500}
下面是我的 JSON 对象(下面只是我感兴趣的值的例子,实际上除了成本还有很多其他参数) 我正在尝试在 experss js 中使用下划线
[
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
我想要如下输出
Joe 950
Tam 500
我试过这个:
var iw={};
iw = Object.keys(myJsonObject.reduce((iw, curr) => {
//iw[curr.author.id] = iw[curr.author.id]
iw[curr.author.id].cost += parseInt(curr.cost);
return iw;
}, iw)).map(key => iw[key]);
console.log("New ::"+iw);
但我没有得到我所希望的:
TypeError: Cannot read property 'cost' of undefined
at Object.keys.myJsonObject.reduce (repl:3:7)
at Array.reduce (<anonymous>)
New ::[object Object]
这可能是一种更简洁的方法,但是一个简单的 forEach 就可以了。显然这里输出的是一个对象,里面包含了想要的结果。
const data = [
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
let a = {};
data.forEach(e => a[e.author.name] ? a[e.author.name] += e.Cost : a[e.author.name] = e.Cost);
console.log(a);
如Fraser所述
您的原始代码存在问题,无法检查该用户是否已经存在于 iw 对象中
您更正的解决方案:
myJsonObject =
[
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "5" }
},
{
"Cost":450,
"author": { id :"2", name : "Joe" , workId: "6" }
},
{
"Cost":150,
"author": { id :"3", name : "Tam" , workId: "7" }
},
{
"Cost":250,
"author": { id :"2", name : "Joe" , workId: "8" }
},
{
"Cost":350,
"author": { id :"3", name : "Tam" , workId: "9" }
}
]
var iw={};
iw = myJsonObject.reduce((iw, curr) => {
iw[curr.author.name] ? iw[curr.author.name] += curr.Cost : iw[curr.author.name] = curr.Cost;
return iw;
}, iw);
console.log(iw);
结果:
{Joe: 950, Tam: 500}