Reduce an array of objects based on an object array property

I need help from some JavaScript/NodeJS experts. I’m using the v14 of NodeJS.

I would like to create a function that takes as an input this kind of array (let’s say that it’s a list of teams):

        name: 'IT',
        tags: ['Department', 'Section', 'Organizational']
        name: 'Male',
        tags: ['Gender', 'Organizational']
        name: 'Foo',
        tags: []

… and returns as an output a list of teams by tag name like this:

// Output:
    Department: 'IT',
    Section: 'IT',
    Organizational: 'IT, Male',
    Gender: 'Male'

The order of object keys as well as the order of the team names don’t matter. I can do this quite easily with a few lines of code, but I’m quite sure that it would be easy to do using a few of our magic map(), reduce() functions and other utilities like spreading.

Anybody can help me achieve this in an optimized way?

Thank you!

19 thoughts on “Reduce an array of objects based on an object array property”

Leave a Comment