E
E
Evtera2020-04-25 18:11:22
JavaScript
Evtera, 2020-04-25 18:11:22

How to scatter the elements of an array over another array?

Hello. There is an array of 4 elements, in my case these are 4 columns from the DOM tree. And there is an array of 12 words. How do I evenly distribute these 12 words across 4 columns, bearing in mind that the number of words may change in the future, but this should not affect the even distribution.

On native js without libraries and other things

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2020-04-25
@Evtera

const words = ['я', 'буду', 'сопровождать', 'свои', 'вопросы', 'на', 'Тостере', 'примерами', 'кода', ',', 'обещаю', '!', ];

const columns = [[], [], [], [],];

words.forEach((word, i) => columns[i % columns.length].push(word));
// 

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question