How to filter the array correctly?

  • 0
    Good afternoon,
    there is an array with nested objects (the data is taken from Excel and can be different every time) - information about employees, each employee has the name of his department in which he works, how to group employees by department correctly?
    [
    {name: 'Alex', department: 'HR'},
    {name: 'Vasilij', department: 'IT'},
    {name: 'Nikolaj', department: 'HR'},
    {name: 'Oleg', department: 'IT'},
    .....
    ]

    As I understand it, first determine all the departments that are on the list, and how to work with this data correctly?
    Thank you
    JavaScript Anonymous, Jan 21, 2020

  • 1 Answers
  • 0
    arr.reduce((acc, n) => ((acc[n.department] ||= []).push(n), acc), {})
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!