Skip to content
Advertisement

Create or reorder an array based on values from array of objects

I have an array of objects, a portion of which looks like this:

JavaScript

[Id] is unique.

[Tag] is not unique.

I need to restructure this so I know which Ids correspond to each Tag. Each Tag should only be listed once. Something like:

JavaScript

It doesn’t necessarily need to be structured like that, but hopefully makes it easier to see what I’m looking for.

I’ve tried looping through the original array, and I’m able to get a unique array of Tags, but I’m not sure how to add/merge the Ids for each Tag:

JavaScript

I may be way off here. Appreciate any help!

Advertisement

Answer

It should be enough just to loop through the result elements and add them to an element identified by the Tag…

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