【问题标题】:how to Repeat word in words如何在单词中重复单词
【发布时间】:2022-01-15 18:42:02
【问题描述】:

let str = 'react';
let newStr = str.split('');
const final = newStr.forEach((word,i) =>{
    const rep = word.repeat(i) + word;
    console.log(rep);
})

我已经重复了,但我想要这个输出 { react eeact aaact cccct ttttt } 而不是这个 {r ee aaa cccc ttttt}

【问题讨论】:

  • word.repeat(i) + str.slice(i);
  • 顺便说一句,你可以再压缩一点:let str = 'react'; Array.prototype.forEach.call(str, (word, i, full) => void console.log(word.repeat(i) + full.slice(i)));。请注意,String.prototype.split 和这都会破坏代理对,这可能不是您想要的。

标签: javascript foreach split repeat word


【解决方案1】:

类似的东西

const str = 'react';
const newStr = str.split('');
const final = newStr.map((word,i,a) => [word.repeat(i), ...a.slice(i)].join(''));

console.log(final);

【讨论】:

  • 你可以使用const final = [...str].map(
  • 刚刚从退出代码继续...
  • 很棒的提示谢谢大家
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 2015-11-17
  • 2014-11-06
  • 2018-07-26
  • 1970-01-01
相关资源
最近更新 更多