Skip to content
Advertisement

Splitting array into groups using typescript/javascript

I have this:

JavaScript

I need to restructure it to this:

JavaScript

the hard part is that the number of objects in each group is set using a variable (in this example the variable would be 4).

How can I achieve this using Typescript/Javascript? Its driving me mad!

Thanks

Advertisement

Answer

Use Array.reduce()

You can run a .reduce() method on your products array, like so:

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