Skip to content

Instantly share code, notes, and snippets.

@VincentDamour
Last active August 14, 2016 13:27
Show Gist options
  • Save VincentDamour/afedf9612c67c4b9c480d6efdff2d57c to your computer and use it in GitHub Desktop.
Save VincentDamour/afedf9612c67c4b9c480d6efdff2d57c to your computer and use it in GitHub Desktop.
var users = [
{ name: "John", age: 30 },
{ name: "Jane", age: 28 },
{ name: "Bill", age: 65 },
{ name: "Emily", age: 17 },
{ name: "Jack", age: 30 }
]
var reducedUsers = _.reduce(users, function (result, user) {
if(user.age >= 18 && user.age <= 59) {
(result[user.age] || (result[user.age] = [])).push(user);
}
return result;
}, {});
// reducedUsers -> {
// 28: [{ name: "Jane", age: 28 }],
// 30: [{ name: "John", age: 30 }, { name: "Jack", age: 30 }]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment