Вставить значения массива после пробелов?

Для вставки значений массива после пробелов в JavaScript можно использовать методы массива и строк. Вот несколько способов, как это можно сделать:

1. Используя методы массива join(), split() и map():

const array = ['apple', 'banana', 'cherry'];
const sentence = 'I like to eat';

const words = sentence.split(' '); // Разбиваем предложение на слова
const newSentence = words.map((word, index) => {
  return index < words.length - 1 ? word + ' ' + array[index % array.length] : word;
}).join(' '); // Объединяем слова в предложение снова

console.log(newSentence); // Выводит "I like to eat apple banana cherry"

2. Используя цикл for:

const array = ['apple', 'banana', 'cherry'];
const sentence = 'I like to eat';
const words = sentence.split(' '); // Разбиваем предложение на слова

let newSentence = '';
for (let i = 0; i < words.length; i++) {
  newSentence += words[i];
  if (i < words.length - 1) {
    newSentence += ' ' + array[i % array.length];
  }
}

console.log(newSentence); // Выводит "I like to eat apple banana cherry"

3. Используя метод reduce():

const array = ['apple', 'banana', 'cherry'];
const sentence = 'I like to eat';
const words = sentence.split(' '); // Разбиваем предложение на слова

const newSentence = words.reduce((acc, word, index) => {
  return index < words.length - 1 ? acc + word + ' ' + array[index % array.length] + ' ' : acc + word;
}, '');

console.log(newSentence); // Выводит "I like to eat apple banana cherry"

Все эти способы достигают одинакового результата - вставки значений массива после пробелов в исходное предложение. Выбор способа зависит от ваших предпочтений и требований к коду.