最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to sum json array key's value if it has one common key - Stack Overflow

programmeradmin1浏览0评论

I have a json object like this. Each of this json object can have mon Product name, for example orgs[0] & orgs[3] have same product name.

"orgs":
    [{
    "Budget Actual Consumption": "12.00",
    "Budget Planned Consumption": "50.00",
    "Product": "Loyalty CO-brand"

}, {
    "Budget Actual Consumption": "11.00",
    "Budget Planned Consumption": "60.00",
    "Product": "Loaylty Rebates"
}, {
    "Budget Actual Consumption": "10.00",
    "Budget Planned Consumption": "7.00",
    "Product": "Loaylty Rebates"
}, {

    "Budget Actual Consumption": "9.00",
    "Budget Planned Consumption": "8.00",
    "Product": "Loyalty CO-brand"
}]

Is it possible to create a new array in such a way that it will have unique multiple object each of which have unique product name & other key will be summed up

For example

var someArray = [{
   name:"Loyalty CO-brand",
   bac:"21" //12+9
   pac:"58" //50+8
},{
  name:"Loaylty Rebates",
   bac:"21" //10+11
   pac:"67" //60+7 

}]

I tried by first creating an array of unique Product,then using forEach twice one inside another. But I could not successfully plete that as I am not sure how will I add up the other keys value when there is a matching Products

I have a json object like this. Each of this json object can have mon Product name, for example orgs[0] & orgs[3] have same product name.

"orgs":
    [{
    "Budget Actual Consumption": "12.00",
    "Budget Planned Consumption": "50.00",
    "Product": "Loyalty CO-brand"

}, {
    "Budget Actual Consumption": "11.00",
    "Budget Planned Consumption": "60.00",
    "Product": "Loaylty Rebates"
}, {
    "Budget Actual Consumption": "10.00",
    "Budget Planned Consumption": "7.00",
    "Product": "Loaylty Rebates"
}, {

    "Budget Actual Consumption": "9.00",
    "Budget Planned Consumption": "8.00",
    "Product": "Loyalty CO-brand"
}]

Is it possible to create a new array in such a way that it will have unique multiple object each of which have unique product name & other key will be summed up

For example

var someArray = [{
   name:"Loyalty CO-brand",
   bac:"21" //12+9
   pac:"58" //50+8
},{
  name:"Loaylty Rebates",
   bac:"21" //10+11
   pac:"67" //60+7 

}]

I tried by first creating an array of unique Product,then using forEach twice one inside another. But I could not successfully plete that as I am not sure how will I add up the other keys value when there is a matching Products

Share Improve this question edited Jun 27, 2016 at 11:58 brk asked Jun 27, 2016 at 11:50 brkbrk 50.3k10 gold badges59 silver badges82 bronze badges 5
  • 1 do you need the spaces behind the number in the strings? – Nina Scholz Commented Jun 27, 2016 at 11:51
  • No i dont need those space, I have edited it – brk Commented Jun 27, 2016 at 11:52
  • please, show us your code and what's not working in it – rafaelcastrocouto Commented Jun 27, 2016 at 11:53
  • 2 I think you have some typos in your desired result.. Both are "Loyalty CO-brand", one should probably be "Loyalty Rebates". And also, I don't think 60+7 = 58, and 50+8 = 67. – Arg0n Commented Jun 27, 2016 at 11:57
  • @Arg0n thanks yes that was a typo, I have updated it – brk Commented Jun 27, 2016 at 12:00
Add a ment  | 

3 Answers 3

Reset to default 4

The solution using Array.forEach function:

var org = { "orgs": [{ "Budget Actual Consumption": "12.00 ", "Budget Planned Consumption": "50.00 ", "Product": "Loyalty CO-brand" }, { "Budget Actual Consumption": "11.00 ", "Budget Planned Consumption": "60.00 ", "Product": "Loaylty Rebates" }, { "Budget Actual Consumption": "10.00 ", "Budget Planned Consumption": "7.00 ", "Product": "Loaylty Rebates" }, { "Budget Actual Consumption": "9.00 ", "Budget Planned Consumption": "8.00 ", "Product": "Loyalty CO-brand" }] },
    newArr = [], bac = 'Budget Actual Consumption', pac = 'Budget Planned Consumption';

orgs.forEach(function (o) {
    if (!this[o.Product]) {
        this[o.Product] = {name: o.Product, bac: +o[bac], pac: +o[pac]};
        newArr.push(this[o.Product]);
    } else {
        this[o.Product]['bac'] += +o[bac];
        this[o.Product]['pac'] += +o[pac];
    }                
}, {});

console.log(JSON.stringify(newArr, 0, 4));

The output:

[
    {
        "name": "Loyalty CO-brand",
        "bac": 21,
        "pac": 58
    },
    {
        "name": "Loaylty Rebates",
        "bac": 21,
        "pac": 67
    }
]

Assuming that the result should be grouped by Product, the you could use an object as hash table for the objects of the result array.

When you omit the requirement of getting strings instead of numbers, the algorithm would be shorter.

var data = { "orgs": [{ "Budget Actual Consumption": "12.00 ", "Budget Planned Consumption": "50.00 ", "Product": "Loyalty CO-brand" }, { "Budget Actual Consumption": "11.00 ", "Budget Planned Consumption": "60.00 ", "Product": "Loaylty Rebates" }, { "Budget Actual Consumption": "10.00 ", "Budget Planned Consumption": "7.00 ", "Product": "Loaylty Rebates" }, { "Budget Actual Consumption": "9.00 ", "Budget Planned Consumption": "8.00 ", "Product": "Loyalty CO-brand" }] },
    grouped = [];

datas.forEach(function (a) {
    if (!this[a.Product]) {
        this[a.Product] = { name: a.Product, bac: '0', pac: '0' };
        grouped.push(this[a.Product]);
    }
    this[a.Product].bac = (+this[a.Product].bac + +a['Budget Actual Consumption']).toString();
    this[a.Product].pac = (+this[a.Product].bac + +a['Budget Planned Consumption']).toString();

}, Object.create(null));

console.log(grouped);

I found a nice solution with Underscore groupBy

http://codepen.io/therealplato/pen/KMWPPN

//foo = {"orgs":[...]}
console.log(foo);
grouped = _.groupBy(foos, grouper);
console.log(grouped);
function grouper(item) {
  return item["Product"];
}

发布评论

评论列表(0)

  1. 暂无评论