groupBy() polyfill in JavaScript

Write the polyfill for the groupBy() method that accepts a collection and iteratee as arguments and returns the object that has grouped the collection values using iteratee as the key.

Example

Input:
groupBy([6.1, 4.2, 6.3], Math.floor);
groupBy(["one", "two", "three"], "length");

Output:
// { 6: [6.1, 6.3], 4: [4.2] }
// { 3: ['one', 'two'], 5: ['three'] }

Here the iteratee can be a function or a property, thus we can check the type of iteratee, and depending upon that we can get the key from it.

The whole logic can be abstracted inside the Array.reduce() method and we can easily aggregate the values.

const groupBy = (values, keyFinder) => {
  // using reduce to aggregate values
  return values.reduce((a, b) => {
    // depending upon the type of keyFinder
    // if it is function, pass the value to it
    // if it is a property, access the property
    const key = typeof keyFinder === 'function' ? keyFinder(b) : b[keyFinder];
    
    // aggregate values based on the keys
    if(!a[key]){
      a[key] = [b];
    }else{
      a[key] = [...a[key], b];
    }
    
    return a;
  }, {});
};
Input:
console.log(groupBy([6.1, 4.2, 6.3], Math.floor)); 
console.log(groupBy(["one", "two", "three"], "length")); 

Output:
// { 6: [6.1, 6.3], 4: [4.2] }
// { 3: ['one', 'two'], 5: ['three'] }