Advertisement
Guest User

Reducing Array

a guest
Nov 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const groups = [
  2.     {
  3.         name : 'Publíco Geral',
  4.         type : 'completo'
  5.     },
  6.     {
  7.         name : 'Publíco Geral',
  8.         type : 'parcial'
  9.     },
  10.     {
  11.         name : 'Maior de 60 anos',
  12.         type : 'completo'
  13.     },
  14.     {
  15.         name : 'Maior de 60 anos',
  16.         type : 'parcial'
  17.     },
  18. ];
  19.  
  20. const count = groups.reduce((result, item) => {
  21.     let group = result.find(group => group.name === item.name && group.type === item.type);
  22.     if(group){
  23.         group.count = group.count++;
  24.     }else(group){
  25.         group = {
  26.             name : item.name,
  27.             type : item.type,
  28.             count : 1
  29.         }
  30.         result.push(group);
  31.     }
  32. });
  33.  
  34. console.log(count);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement