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));
Also in JavaScript:
- react npm build not working
- angular cors issue
- js disable event listener
- angular how to copy text with button
- javascript linkify string text
- search string in file node
- angular ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
- javascript go to previous page
- node js send fcm
- transition event listener does not work
- javascript function that make a choice
- find method javascript
- angular material chips
- how to move an element of an array in javascript
- javascript get element height
- javascript array add
- how to save thing in cookie js
- javascript get array object by id
- python save list to json
- Material-ui Accessibility icon
- Warning: Couldn't read data from file "accounts.json", this makes an empty
- escaped json to json javascript
- javascript get classlist of element
- watch file in changes in webpack