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

5
  • 1
    do you need the spaces behind the number in the strings? Commented Jun 27, 2016 at 11:51
  • No i dont need those space, I have edited it Commented Jun 27, 2016 at 11:52
  • please, show us your code and what's not working in it 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. Commented Jun 27, 2016 at 11:57
  • @Arg0n thanks yes that was a typo, I have updated it Commented Jun 27, 2016 at 12:00

3 Answers 3

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
    }
]
Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

What do the + bits before the 2 values ( +this[a.Product].bac + +a['Budget Actual Consumption'] ) do in these lines? Thanks.
its an type cast to number.
Thanks. Read some more about it here: stackoverflow.com/questions/12120802/…
@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.
@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.
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

1 Comment

Now that you have one array per type, your next step is to use _.map() to calculate the stats for that particular type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.