Skip to content
Advertisement

Repeat elements in array based on length of another array

I have two arrays of string with dynamic lenght, for example:

JavaScript

and I would like that colors.lenght >= categories.lenght. Missing element of colors should be repeated. Example:

JavaScript
JavaScript
JavaScript

How can I do that?

I start creating this function but I don’t know how to repeat colors in the right way:

JavaScript

Advertisement

Answer

you can do something like this

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