const insert = (str, indices, substr = ' ') =>
Array.prototype.reduce.call(
str,
(acc, n, i) => acc + (indices.includes(i) ? substr : '') + n,
''
);
// или
const insert = (str, indices, substr = ' ') => [...indices]
.sort((a, b) => b - a)
.reduce((acc, n) => (acc.splice(n, 0, substr), acc), [...str])
.join('');
// или
const insert = (str, indices, substr = ' ') => []
.concat(0, indices)
.sort((a, b) => a - b)
.map((n, i, a) => str.slice(n, a[i + 1]))
.join(substr);
// или
const insert = (str, indices, substr = ' ') => indices.length
? str.replace(RegExp(indices.map((n, i) => `(?<=^.{${n}})`).join('|'), 'g'), substr)
: str;
Пользоваться этим, понятное дело, так:
str = insert(str, [ 1, 3, 6, 8, 10 ]);
.