face turn face turn - 3 years ago 162
TypeScript Question

Group data based on property javascript

I have json array as follows,

{
"lstMonths": [
"September"
],
"lstBalances": [
{
"AccountName": "john",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "-28.11"
}
]
},
{
"AccountName": "sanga\n",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "100"
}
]
},
{
"AccountName": "mahela",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "400"
}
]
},
{
"AccountName": "sachin",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "600"
}
]
},
{
"AccountName": "dhoni",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "-500"
}
]
}
]
}


i want to group by AccountName and form the output as,

{ data: [28.11], label: 'john' },
{ data: [100], label: 'sanga' },
{ data: [400], label: 'mahela' },
{ data: [400], label: 'sachin' }
{ data: [400], label: 'dhoni' }


if there are two months, say october in above then

{ data: [28.11,3], label: 'john' },
{ data: [100,4], label: 'sanga' },
{ data: [400,4], label: 'mahela' },
{ data: [400,4], label: 'sachin' }
{ data: [400,5], label: 'dhoni' }


i tried to group like as above. any help guys?

here is my code:

var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
console.log(groupBy(response.lstMonths, 'length'));


please provide a way to find the best value.

EDIT

json for more months,


{ "lstMonths": [
"September" ], "lstBalances": [
{
"AccountName": "john\n",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "-28.11"
},
{
"MonthName": "August",
"Amount": "-28.11"
}
]
},
{
"AccountName": "sanga\n",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "100"
},
{
"MonthName": "August",
"Amount": "233"
}
]
},
{
"AccountName": "mahela",
"AccountBalanceOnMonth": [
{
"MonthName": "September",
"Amount": "400"
},
{
"MonthName": "August",
"Amount": "32"
}
]
} ] }

Answer Source

Assuming,

  • only one array with data,
  • for every month is a value,
  • all months are sorted
  • result months should have the same sorting,

then you could take the array with accounts and iterate the accounts for building a new object for each account and take the balance values for the data array.

var data = [{ lstMonths: ["September"], lstBalances: [{ AccountName: "john", AccountBalanceOnMonth: [{ MonthName: "September", Amount: "-28.11" }, { MonthName: "August", Amount: "-28.11" }] }, { AccountName: "sanga", AccountBalanceOnMonth: [{ MonthName: "September", Amount: "100" }, { MonthName: "August", Amount: "233" }] }, { AccountName: "mahela", AccountBalanceOnMonth: [{ MonthName: "September", Amount: "400" }, { MonthName: "August", Amount: "32" }] }] }],
    grouped = data[0].lstBalances.map(function (o) {
        return {
            data: o.AccountBalanceOnMonth.map(function (p) { return +p.Amount; }),
            label: o.AccountName
        };
    });
    
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download