Skip to content
Advertisement

Reducer function for a nested array with conditions

I’m struggling to write a reducer function for the nested object that I have.

JavaScript

queryResult can have many Parents. Every Parent has items, and every item has subitems with their .ids.

How to write a reducer that would reduce the queryResult by taking in the parentId and the itemId and returning the array of subitems? For example, for parentId = ABC and for the itemId = 456 I need a result that looks like:

JavaScript

Note: all the IDs are random, there is no logic there whatsoever.

Advertisement

Answer

You can reduce queryResult to a map of parentId-itemId as the key and subitems as the value as follows:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement