I am trying to merge/filter an array by shop name and it’s status (group by shop & status), also need to sum it’s rent_toaal, latefee_total, amount_total and finally count total_shops
I know it’s easy for you guys, help me achieving this.
Thanks for your valuable time in advance
var arr = [
{
"shop": "Dumka Town",
"status": "paid",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Dumka Town",
"status": "paid",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Dumka Town",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Dumka Town",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Dumka Town",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Goushala road",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Goushala road",
"status": "paid",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
},
{
"shop": "Goushala road",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 100
},
{
"shop": "Shiv Pahad",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101
}
]
`
Required output
[
{
"shop": "Dumka Town",
"status": "paid",
"rent_total": 200,
"latefee_total": 2,
"amount_total": 202,
"total_shops": 2
},
{
"shop": "Dumka Town",
"status": "pending",
"rent_total": 300,
"latefee_total": 3,
"amount_total": 303,
"total_shops": 3
},
{
"shop": "Goushala road",
"status": "paid",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101,
"total_shops": 1
},
{
"shop": "Goushala road",
"status": "pending",
"rent_total": 200,
"latefee_total": 2,
"amount_total": 201,
"total_shops": 2
},
{
"shop": "Shiv Pahad",
"status": "pending",
"rent_total": 100,
"latefee_total": 1,
"amount_total": 101,
"total_shops": 1
}
]
I tried this but it’s just filtering by shop name
var result = [];
res.forEach(function (a) {
if (!this[a.area, a.status]) {
this[a.area] = { area: a.area, status: a.status, rent_total: 0, latefee_total: 0, amount_total: 0 };
result.push(this[a.area]);
}
else {
this[a.area].rent_total += a.rent_total;
this[a.area].latefee_total += a.latefee_total;
this[a.area].amount_total += a.amount_total;
}
}, Object.create(null));
console.log(result);
I need short code to achieve this
2
Answers
You are missing taking both the shop name and the status into consideration when grouping the data and also add a total to keep track of the total number of shops
You may find it easier to create a dictionary using the
shop/status
values as keys……and then you can update the objects associated with those keys on each iteration.
Perhaps:
reduce
over the array of data, build up a dictionary of new/updated objects using theshop/status
values as keys, and then extract those objects into a new array withObject.values
.Additional documentation
Nullish Coalescing Assignment (??=)
Template string