4

How do I repeat the colors array in order, given an unknown length of items?

const items = [1, 2, ...n]
const colors = ['blue', 'green', 'red']

// return ['blue', 'green', 'red', 'blue', 'green'] where items.length = 5

2

6 Answers 6

7

const items = [1, 2, 3,4,5,6,7,8]
const colors = ['blue', 'green', 'red']

const result = items.map((_,i) => colors[i%colors.length]);

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I was stuck in a block. Many thanks! Edit: I'll accept the answer as soon as I'm allowed :)
3

You can map over a new array with the length you want and take the modulus of the index by the length of the colors array. You do not seem to need the items array at all.

let len = 5;
const colors = ['blue', 'green', 'red'];
const res = Array(len).fill().map((_,idx)=>colors[idx % colors.length]);
console.log(res);

Array.from can also be used in this case.

let length = 5;
const colors = ['blue', 'green', 'red'];
const res = Array.from({length}, (_,idx)=>colors[idx % colors.length]);
console.log(res);

2 Comments

Or similarly, Array.from({length: 5}, (_, i) => colors[i % colors.length])
@ScottSauyet Thanks for the suggestion.
3

When the target size is large, you may get better performance by doubling the array over and over again:

function stretch(arr, n) {
  while (arr.length < n) arr = arr.concat(arr);
  return arr.slice(0, n);
}

const items = [1, 2, 3, 4, 5];
const colors = ['blue', 'green', 'red'];

console.log(stretch(colors, items.length));

1 Comment

A nice approach for a large array. For clarity, I would choose a map / Array.from solution over this unless I expected large arrays. But when wanted, this is quite useful.
3

Create a new array and map the values by using the remainder operator %.

const
    items = [1, 2, 3, 4, 5, 6, 7, 8],
    colors = ['blue', 'green', 'red'],
    result = Array.from(items, (_, i) => colors[i % colors.length]);

console.log(result);

2 Comments

Do you find this cleaner than a map call?
yes, because it take only a part structure (the length) of the original array. by using map, i miss the part where an element is used for the result. (this may apply here as well ...)
2

You can take mode for index so you can repeat it.

const items = [1, 2, 3,4,5,6];
const colors = ['blue', 'green', 'red'];

var result=[];
items.forEach(myFunction)

function myFunction(item, index) {
  console.log(colors[(index%colors.length)])
  result.push(colors[(index%colors.length)])
}

2 Comments

That's a map in disguise. You might as well just use it directly.
Now you have a function with side effects.
1

Using map()

const  items  = [1, 2, 3, 4, 5, 6, 7, 8],
       colors = ['blue', 'green', 'red'];
output = items.map((_,i) => colors[i % colors.length])
     
console.log(output);

3 Comments

What does the spread add here? See the answer from Ali for comparison.
I'm suggesting that your same code would work if you removed the initial [... and trailing ]. The map call already creates a new array that does what we want. The spread is entirely superfluous.
You can also remove True or Correct from your last comment!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.