1

I have this data below(i have many record like this: the year org_name etc are many as well)

[{
    "year": "2015",
    "org_name": "MOOE",
    "suborg_name": "Forms Expenses",
    "amt": "2",
    "org_id": "4",
    "suborg_id": "24"
}, {
    "year": "2016",
    "org_name": "MOOE",
    "suborg_name": "Forms Expenses",
    "amt": "1",
    "org_id": "4",
    "suborg_id": "24"
},{
    "year": "2015",
    "org_name": "MOOE1",
    "suborg_name": "Forms Expenses1",
    "amt": "2",
    "org_id": "41",
    "suborg_id": "241"
}, {
    "year": "2016",
    "org_name": "MOOE1",
    "suborg_name": "Forms Expenses1",
    "amt": "1",
    "org_id": "41",
    "suborg_id": "241"
}]

I want to get this format

[{
    "org_name": "MOOE",
    "suborg_name": "Forms Expenses",
    "suborg_id": "24",
    "org_id": "4",
    "Year": [{
        "year": "2015",
        "amt": "2"
    }, {
        "year": "2016",
        "amt": "1"
    }]
},{
    "org_name": "MOOE1",
    "suborg_name": "Forms Expenses1",
    "suborg_id": "241",
    "org_id": "41",
    "Year": [{
        "year": "2015",
        "amt": "2"
    }, {
        "year": "2016",
        "amt": "1"
    }]
}]

What i want is to group them when the org_name suborg_name suborg_id org_id are the same. There will be different year and value for those data.

I am working on this :

var b={result{}};
for (i=0;i<a.result.length;i++)
{
    b.result[a.result[i].category] = b.result[a.result[i].category] || {};//use existing, or create new object
    b.result[a.result[i].category][a.result[i].event] = a.result[i].result;//add property for event type, assign value
}

From : link

But i cant make it work.Any easier implementation is appreciated

Community
  • 1
  • 1
Brownman Revival
  • 3,620
  • 9
  • 31
  • 69

0 Answers0