1

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

0

2 Answers 2

1

You could create a new key made of the values of the wanted properties.

function groupArr(arr, ...props) {
    return arr.reduce(function (groups, item) {
        const key = props.map(k => item[k]).join('|');
        groups[key] = groups[key] || [];
        groups[key].push(item);
        return groups;
    }, {});
}
var array = [{ 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 }, { date: "2018-01-01", website: "example.com", revenue: 340 }];
    grouped = groupArr(array, "date", "website");

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

You can do something like:

let arr = [{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}];


function groupArr(arr, prop) {
  prop = Array.isArray(prop) ? prop : [prop];         //Make sure that prop is an array. Convert to array if not

  return arr.reduce(function(groups, item) {
    const val = prop.map(o => item[o]).join('-');     //Map and Concat all values of prop and use as key

    groups[val] = groups[val] || [];
    groups[val].push(item);
    return groups
  }, {})
}

//Will work if prop is array
let result = groupArr(arr, ["date", "website"]);
console.log(result);

//Will work if string
let result2 = groupArr(arr, "date");
console.log(result2);

1 Comment

Thanks, also a great solution which shows passing second parameter as array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.