2

I have a json object like this. Each of this json object can have common 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 complete that as I am not sure how will I add up the other keys value when there is a matching Products

brk
  • 48,835
  • 10
  • 56
  • 78

3 Answers3

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

org.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
    }
]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
3

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 = [];

data.orgs.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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • What do the `+` bits before the 2 values ( `+this[a.Product].bac + +a['Budget Actual Consumption']` ) do in these lines? Thanks. – Mohit Bhardwaj Jun 27 '16 at 12:45
  • its an type cast to number. – Nina Scholz Jun 27 '16 at 12:46
  • 1
    Thanks. Read some more about it here: http://stackoverflow.com/questions/12120802/explain-var-and-var-unary-operator-in-javascript – Mohit Bhardwaj Jun 27 '16 at 12:50
  • @NinaScholz Maybe `bac` and `pac` souldn't be string then your to string and reassignments would be unnecessary. BTW that could happen with `JSON.stringify`. – Morteza Tourani Jun 28 '16 at 00:01
  • 1
    @mortezaT, the requirement shows strings, so i do strings. `JSON.stringify` does not convert numbers to string, instead it put the whole object into a string. – Nina Scholz Jun 28 '16 at 05:42
2

I found a nice solution with Underscore groupBy

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

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

example output of _groupBy

Plato
  • 10,812
  • 2
  • 41
  • 61
  • Now that you have one array per type, your next step is to use _.map() to calculate the stats for that particular type – Plato Jun 27 '16 at 12:11