Skip to content
Advertisement

Organize duplicates into individual array that is ordered

I have an array with numbers. I would like to put the numbers in order and create new array with duplicats in the same array(array in array). Can someone please help me step by step. I would really like to understand

JavaScript

Advertisement

Answer

You can extract unique values using Set, then sort them (because sorting an array of arrays is more complex), then use array.reduce to acquire all the items in the original array and push either the single value if unique, otherwise the array of values (not sure why you need that, but still..)

Further documentation reference:

Working code below:

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