I'm trying to group array of objects by passing the key i want to group by as a parameter to a function i wrote, so for example if i have this array of 3 objects:
[{date: "2018-01-01", website: "example.com", revenue: 100},
{date: "2018-01-01", website: "example2.com", revenue:200},
{date: "2018-01-02", website: "example.com", revenue: 300}]
and i will pass them to my function:
groupArr(arr, prop) {
return arr.reduce(function (groups, item) {
const val = item[prop];
groups[val] = groups[val] || [];
groups[val].push(item);
return groups
}, {})
}
the result will be :{2018-01-01: Array(2), 2018-01-02: Array(1)}
but now i'm trying to figure out how can i change this function in a way that i can pass two parameters, for example date and website: groupArr(arr,["date","website"]
so that my result will include group by two parameters, which in my case will end up like this:
{{[2018-01-01,"example.com"]: Array(1),[2018-01-01,"example2.com"]: Array(1), 2018-01-02: Array(1)}
i'm presenting the result key's as an array for convenience purposes, not sure if thats the right way to do so. any idea how can i achieve that? thanks