Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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));
Comment

PREVIOUS NEXT
Code Example
Javascript :: cannot create an instance of an abstract class httphandler angular 
Javascript :: jquery.validate.unobtrusive dynamic content 
Javascript :: jquery user function override 
Javascript :: node js if 
Javascript :: redux http library 
Javascript :: difference between Redis and StrictRedis 
Javascript :: getElementbyhref 
Javascript :: pluton 
Javascript :: javascript and python graphs for data analysis 
Javascript :: check a divide condition and print msg javascript 
Javascript :: print("Google") in JavaScript 
Javascript :: javascript on the fly form submit 
Javascript :: js map vs react js map 
Javascript :: whait till src img has loaded angular 
Javascript :: print from mongo console to file sample.json 
Javascript :: simple editor reacct 
Javascript :: Merger Douplicate array Object 
Javascript :: asp.net run javascript on page load 
Javascript :: how to find dublicates in string 
Javascript :: compare two array value in javascript 
Javascript :: press enter reaction js 
Javascript :: convert fetch promise to observable 
Javascript :: setstate to false after 10 sec react native 
Javascript :: 4.7.1. The String Operator +¶ 
Javascript :: angualr js busy when routing 
Javascript :: string recurive in javascript 
Javascript :: discord.js mention 
Javascript :: How to update Code Mirror data on modal show 
Javascript :: array destructuring mdn 
Javascript :: validate date 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =