Skip to content
Advertisement

How can I remove a number of objects in an array?

I have an array like this :

JavaScript

I only want two of each of category object in the array and remove the rest If the number of objects of that category is over 2 and get something like this :

JavaScript

How can I remove the rest of the objects of a specific category If the number of those objects in the array is over 2 ?

Advertisement

Answer

JavaScript

The final array is in ‘filteredArray’. The original array is not changed, so you may want to do the following:

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