Reputation: 73
I'm currently trying to map and reduce functions to flatten out a multidimensional array.
This is a mock example data set:
data: [
{
label: "Sort-01"
data: [
{
label: "OCT-2017"
weight: 2304
},
{
label: "NOV-2017"
weight: 1783
}
]
},
{
label: "Sort-02"
data: [
{
label: "OCT-2017"
weight: 4785
},
{
label: "NOV-2017"
weight: 102
}
]
},
......
]
I know in order to map-reduce by sort number I can use:
data.map(sort => sort.data.reduce((a,b) => a.weight + b.weight));
However, I want to reduce by the month instead of sort number.
I would appreciate any help.
Thanks,
Jay
Upvotes: 3
Views: 3515
Reputation: 192252
Use Array#map to get the an array of the data
properties array, then flatten with Array#concat and spread. Use Array#reduce to collect the values into a Map, then use Map#values, and the spread syntax to convert back to an array:
const array = [{"label":"Sort-01","data":[{"label":"OCT-2017","weight":2304},{"label":"NOV-2017","weight":1783}]},{"label":"Sort-02","data":[{"label":"OCT-2017","weight":4785},{"label":"NOV-2017","weight":102}]}];
const result = [... // spread the values iterator to an array
[].concat(...array.map(({ data }) => data)) // map the array into an array of data arrays
.reduce((m, { label, weight }) => {
// take item if label exists in map, if not create new
const item = m.get(label) || { label, weight: 0 };
item.weight += weight; // add the weight
return m.set(label, item); // set the item in the map
}, new Map).values()] // get the values iterator
console.log(result);
Here is a spreadless version:
const array = [{"label":"Sort-01","data":[{"label":"OCT-2017","weight":2304},{"label":"NOV-2017","weight":1783}]},{"label":"Sort-02","data":[{"label":"OCT-2017","weight":4785},{"label":"NOV-2017","weight":102}]}];
const helper = Object.create(null);
const result = [].concat.apply([], array.map(({ data }) => data)) // map the array into an array of data arrays, and flatten by apply concat
.reduce((r, { label, weight }) => {
// if label is not in helper, add to helper, and push to r
if(!helper[label]) {
helper[label] = { label, weight: 0 };
r.push(helper[label]);
}
helper[label].weight += weight; // add the weight to the object
return r;
}, []) // get the values iterator
console.log(result);
Upvotes: 2
Reputation: 1619
You can use reduce or concat (see Ori's answer) to get a flattened data array. From there reassign your year label as key and weight as value. then pass to another reducer in order to total your weights by month.
const data = [{label: "Sort-01",data: [{label: "OCT-2017",weight: 2304,},{label: "NOV-2017",weight: 1783,},],},{label: "Sort-02",data: [{label: "OCT-2017",weight: 4785,},{label: "NOV-2017",weight: 102,},],},];
const flatten = (acc, cur) => {
cur.data.forEach(val => acc.push(val));
return acc;
};
const monthMap = ({ label, weight }) => ({ [label]: weight });
const reducer = (acc, cur) => {
const key = Object.keys(cur)[0]
if (!acc.hasOwnProperty(key)) { acc[key] = 0 }
acc[key] += cur[key];
return acc;
};
let x = data.reduce(flatten, []).map(monthMap).reduce(reducer, {});
console.log(x);
Upvotes: 1