groupBy
Given an array of objects and a property name, group the objects by the value of that property. The output should be an object where keys are the property values and values are arrays of the objects.
Examples
groupBy([{type: 'fruit', name: 'apple'}, {type: 'veg', name: 'carrot'}, {type: 'fruit', name: 'banana'}], 'type') → {fruit: [...], veg: [...]}
Starter code
function groupBy(arr, key) {
}
Complexity of the optimal solution
Time O(n), space O(n). The `reduce` method iterates through the 'n' elements of the array once. The resulting object and its nested arrays will contain all 'n' original objects. Therefore, both time and space complexity are linear.