Skip to content
Advertisement

How to grouping array of data by its category in javascript

This is my data:

JavaScript

How can I transform this data to grouping each pet according to its species and gender like like this:

JavaScript

My code is:

JavaScript

the result from my code is:

JavaScript

I expect the final result is like the second array above. Does any one know how to do this better and explaining what I’m missing. Thanks!!

Advertisement

Answer

I think visualizing the problem with the images was clearer, but okay…

Anyway I wrote the function below which should be the simplest to understand.

JavaScript

I’m currently reviewing your code to try to make it work!

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