Reputation: 1749
I wanted to use higher order functions such as Map, Filter, Reduce.
My problem is I have an array which is not organised so I had to perform a loop operation to get the result I wanted. An array has two fields one is _id
and totalCount
which is an array the length will be minimum 0 to 3.
The totalCount
each iteration consists of two fields one is orderstatus and another is total I want to perform a normal if else condition if orderstatus is "Merchant" or "driver" or "user" I want to take that iterated total and store it in my array like MerchantCount
.
This is my code:
var arr = [{
_id: "2017-12-08",
totalcount: [{
orderstatus: "MERCHANT",
total: 1
}]
},
{
_id: "2017-12-02",
totalcount: [{
orderstatus: "USER",
total: 2
}]
},
{
_id: "2017-12-06",
totalcount: [{
orderstatus: "DRIVER",
total: 1
}]
},
{
_id: "2017-12-07",
totalcount: [{
orderstatus: "MERCHANT",
total: 3
}]
},
{
_id: "2017-12-04",
totalcount: [{
orderstatus: "DRIVER",
total: 1
},
{
orderstatus: "MERCHANT",
total: 2
},
{
orderstatus: "USER",
total: 1
}
]
}
]
The loop am performing:
for (let i = 0; i < recentUserCount.length; i++) {
for (let j = 0; j < recentUserCount[i].totalcount.length; j++) {
if (recentUserCount[i].totalcount[j].usertype == "DRIVER") {
recentUserCount[i].DRIVERCount = recentUserCount[i].totalcount[j].total;
} else if (recentUserCount[i].totalcount[j].usertype == "USER") {
recentUserCount[i].USERCount = recentUserCount[i].totalcount[j].total;
} else if (recentUserCount[i].totalcount[j].usertype == "MERCHANT") {
recentUserCount[i].MERCHANTCount = recentUserCount[i].totalcount[j].total;
}
}
}
The result will be roughly like below:
0 : {_id: "2017-12-08", totalcount: Array(1), MERCHANTCount: 3, $$hashKey: "object:316"}
1 : {_id: "2017-12-07", totalcount: Array(1), MERCHANTCount: 3, $$hashKey: "object:317"}
2 : {_id: "2017-12-06", totalcount: Array(1), DRIVERCount: 1, $$hashKey: "object:318"}
3 : {_id: "2017-12-04", totalcount: Array(3), DRIVERCount: 1, MERCHANTCount: 2, USERCount: 1, …}
I want the same operation to be performed using Map/Filter or Reduce method.
Upvotes: 0
Views: 619
Reputation: 7253
Pretty easy with ES6 syntax and map/reduce, Full functional snippet:
var arr = [{_id:"2017-12-08",totalcount:[{orderstatus:"MERCHANT",total:1}]},{_id:"2017-12-02",totalcount:[{orderstatus:"USER",total:2}]},{_id:"2017-12-06",totalcount:[{orderstatus:"DRIVER",total:1}]},{_id:"2017-12-07",totalcount:[{orderstatus:"MERCHANT",total:3}]},{_id:"2017-12-04",totalcount:[{orderstatus:"DRIVER",total:1},{orderstatus:"MERCHANT",total:2},{orderstatus:"USER",total:1}]}];
const result = arr
.map(({_id, totalcount}) => ({
totalcount,
_id,
...totalcount.reduce((acc, {orderstatus, total}) =>
({...acc, [`${orderstatus}count`]: total}), {})
}))
console.log(result)
https://jsbin.com/kiwalozuxa/edit?html,js,console,output
EDIT: The spread operator (...) is used to spread the keys/values of an objet (or values of an array). totcalcount.reduce
here return an object:
{
x: "x",
y: "y"
}
Since it's not technically valid in javascript to have an objet of this form:
{
a: "a",
b: "b",
{
x: "x",
y: "y"
}
}
I use the spread operator to merge my "child" object into the parent objet and produce:
{
a: "a",
b: "b",
x: "x",
y: "y"
}
I use the same technique in the reducer.
Upvotes: 4
Reputation: 386680
You could shrink it to two nested forEach loops and take orderstatus
as part of the new property.
var totalReference = { MERCHANT: 'MERCHANTCount', USER: 'USERCount', DRIVER: 'DRIVERCount' },
array = [{ _id: "2017-12-08", totalcount: [{ orderstatus: "MERCHANT", total: 1 }] }, { _id: "2017-12-02", totalcount: [{ orderstatus: "USER", total: 2 }] }, { _id: "2017-12-06", totalcount: [{ orderstatus: "DRIVER", total: 1 }] }, { _id: "2017-12-07", totalcount: [{ orderstatus: "MERCHANT", total: 3 }] }, { _id: "2017-12-04", totalcount: [{ orderstatus: "DRIVER", total: 1 }, { orderstatus: "MERCHANT", total: 2 }, { orderstatus: "USER", total: 1 }] }];
array.forEach(function (o) {
o.totalcount.forEach(function (p) {
if (totalReference[p.orderstatus]) {
o[totalReference[p.orderstatus]] = p.total;
}
});
});
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 0
Reputation: 73251
I'd reduce and assign it
let res = arr.reduce((a, b) => {
let totals = b.totalcount.reduce((x, y) => {
x[y.orderstatus + 'Count'] = (x[y.orderstatus + 'Count'] || 0) + y.total;
return x;
}, {});
return a.concat(Object.assign(b, totals));
}, []);
var arr = [{_id:"2017-12-08",totalcount:[{orderstatus:"MERCHANT",total:1}]},{_id:"2017-12-02",totalcount:[{orderstatus:"USER",total:2}]},{_id:"2017-12-06",totalcount:[{orderstatus:"DRIVER",total:1}]},{_id:"2017-12-07",totalcount:[{orderstatus:"MERCHANT",total:3}]},{_id:"2017-12-04",totalcount:[{orderstatus:"DRIVER",total:1},{orderstatus:"MERCHANT",total:2},{orderstatus:"USER",total:1}]}];
let res = arr.reduce((a, b) => {
let totals = b.totalcount.reduce((x, y) => {
x[y.orderstatus + 'Count'] = (x[y.orderstatus + 'Count'] || 0) + y.total;
return x;
}, {});
return a.concat(Object.assign(b, totals));
}, []);
console.log(res);
Upvotes: 1