Answers for "The value associated with each key will be an array consisting of all the elements that resulted in that return value when passed into the callback."

0

The value associated with each key will be an array consisting of all the elements that resulted in that return value when passed into the callback.

function groupBy(array, callback) {

  return array.reduce((acc,cur) => {
    let key = callback(cur)
    acc[key] = acc[key] || [];
    acc[key].push(cur)
    return acc;
  },{})
  
}

const decimals = [1.3, 2.1, 2.4];

const floored = function(num) {
  return Math.floor(num);
};

console.log(groupBy(decimals, floored));
Posted by: Guest on January-11-2021

Code answers related to "The value associated with each key will be an array consisting of all the elements that resulted in that return value when passed into the callback."

Code answers related to "Javascript"

Browse Popular Code Answers by Language