I have a strange situation for filtering duplicates.
I have to turn this
[
{name: A, number: 1, order: 1},
{name: B, number: 1, order: 2},
{name: C, number: 1, order: 3},
{name: D, number: 2, order: 4},
{name: E, number: 2, order: 5},
{name: F, number: 1, order: 6}
]
Into
[
[
{name: A, number: 1, order: 1},
{name: B, number: 1, order: 2},
{name: C, number: 1, order: 3},
],
[
{name: D, number: 2, order: 4},
{name: E, number: 2, order: 5},
],
[
{name: F, number: 1, order: 6}
]
]
I have been able to put duplicates into their own arrays but this extra step has me scratching my head.
2
Answers
Here’s an example function that organizes the array based on the "number" property:
then you can use it like the following:
I hope this is what you are looking for 🙂
This approach creates a new subarray each time it notices that the current object’s number property is not the same as the last object’s number property. Otherwise, it adds the current object to the most recent subarray.
It adds these subarrays to the accumulator of the reduce operation. Note that this approach uses a comma expression to avoid needing braces and the "return" keyword.