【发布时间】:2020-12-06 05:51:38
【问题描述】:
我有一个充满字符串的数组,我想循环并用 '' 替换任何出现的 '123'。
期望的结果是:['hello', 'cats', 'world', 'dogs']
let arr = ['he123llo', 'cats', 'wor123ld', 'dogs'];
arr.forEach(x => {
x.replace('123', '');
});
【问题讨论】:
-
const replacedStrings = arr.map(word => word.replace(/123/g, '')) -
forEach()返回undefined,你应该使用Array.prototype.map()insted,顺便说一句,如果你想替换,最好使用.replace(/123/g, '')所有次出现不需要的子串
标签: javascript