daily pastebin goal
95%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const filterAndGroup = (collection, min, max) => {
  2.   return collection
  3.     .filter(item => item.age >= min && item.age <= max)
  4.     .reduce(
  5.       (acc, { age, name, gender }) => ({
  6.         ...acc,
  7.         [gender]: [...acc[gender], { age, name }]
  8.       }),
  9.       { male: [], female: [] }
  10.     );
  11. };
  12.  
  13. // Sample data and usage example
  14. const data = [
  15.     {age: 31, name: 'Jennifer', gender: 'female'},
  16.     {age: 42, name: 'Max', gender: 'male'},
  17.     {age: 15, name: 'Amy', gender: 'female'},
  18.     {age: 18, name: 'Lucy', gender: 'female'},
  19.     {age: 37, name: 'Carl', gender: 'male'},
  20.     {age: 32, name: 'Susanne', gender: 'female'},
  21. ];
  22.  
  23. filterAndGroup(data);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top