Skip to content
Advertisement

How to duplicate an object in an array by given quantity, ES6 and above

I’m trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.

JavaScript

My code:

JavaScript

So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?

Advertisement

Answer

You could use .fill() and .flatMap().

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