简体   繁体   English

循环通过 JSON object 并通过独特的深度属性对值求和

[英]Loop through JSON object and sum values by unique deep properties

Below is my JSON object (Below is just example of values I am interested in, in reality along with cost there are many other paramters) I am trying to do this in experss js and using underscore下面是我的 JSON object (下面只是我感兴趣的值的示例,实际上除了成本还有许多其他参数)我正在尝试在 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" }
    }
]

I want the output as below我想要 output 如下

Joe 950乔950
Tam 500谭500

I tried this:我试过这个:

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);

But I didn't get what I hoped for:但我没有得到我所希望的:

TypeError: Cannot read property 'cost' of undefined
    at Object.keys.myJsonObject.reduce (repl:3:7)
    at Array.reduce (<anonymous>)
New ::[object Object]

Probably a much cleaner way to do this, but a simple forEach would work.可能是一种更清洁的方法,但一个简单的 forEach 就可以了。 Obviously the output here is an object that contains the desired result.显然,这里的 output 是包含所需结果的 object。

 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);

as Fraser said正如弗雷泽所说

your original code had problem of not checking if that user already exists in the iw object already or not您的原始代码存在不检查该用户是否已经存在于 iw object 中的问题

your corrected solution:您更正的解决方案:

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);

Result:结果:

{Joe: 950, Tam: 500}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM