在 Javascript 中的字符串文字数组上使用 .replaceAll()

Using The .replaceAll() on a string literal array in Javascript

目标:使用数组的方法去除字符串get, right, the, first, time 并将它们替换为 secretMessage 数组中已知的单个字符串。

Plan 定义一个函数,用于在雨中发送文本、旧字符串和新字符串,并使用 .replaceall() 方法并传递每个不同的参数一次一个,每次调用都重新分配数组。

到目前为止的代码

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){=
  let updatedArray= orginalArray.replaceAll(oldString,updatedString);//Not Sure If I need to wrap this in []
  return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);

当前结果

let updatedArray= orginalArray.replaceAll(oldString,updatedString);
                               ^

类型错误:

orginalArray.replaceAll is not a function at replaceWords

replaceAll() 适用于字符串而不是数组。试试下面的例子。

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){=
  let updatedArray= orginalArray.join().replaceAll(oldString,updatedString).split();
  return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);

正如, replaceAll()所述,它是一种字符串方法,它不适用于数组。


所以我们需要一个方法


幸运的是 map() 正是这样做的:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){
  return orginalArray.map(item => {
    return item.replaceAll(oldString, updatedString)
  });
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);

replaceAllString 函数,而不是 Array 函数。如果要替换可能作为数组中的值存在的单词,则必须遍历该数组。您的代码如下所示:

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
    

function replaceWords(orginalArray,oldString,updatedString){
  /* because your array values are just single words 
  and you want to replace the whole word with another whole word, 
  you can use string comparison instead of replaceAll() */
  
  let updatedArray= orginalArray.map(string => (string == oldString ? updatedString : string));
  return updatedArray;
}
secretMessage = replaceWords(secretMessage,'get','know');
secretMessage = replaceWords(secretMessage,'right','know');
secretMessage = replaceWords(secretMessage,'the','know');
secretMessage = replaceWords(secretMessage,'first','know');
secretMessage = replaceWords(secretMessage,'time','know');

console.log(secretMessage);

只是为了“好玩”,同时使用数组方法(Array.prototype.join())replaceAll()(String.prototype.replaceAll())和关闭的解决方案按照您最初的想法,可能是:

function replaceWords(arr, oldStr, newStr) {
  return arr.join(' ')          // join all the array items into a single space-separated string
    .replaceAll(oldStr, newStr) // call `replaceAll()` on the newly created string
    .split(' ');                // split the string back into an array
}

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];

secretMessage = replaceWords(secretMessage, 'get', 'know');
secretMessage = replaceWords(secretMessage, 'right', 'know');
secretMessage = replaceWords(secretMessage, 'the', 'know');
secretMessage = replaceWords(secretMessage, 'first', 'know');
secretMessage = replaceWords(secretMessage, 'time', 'know');

console.log(secretMessage);

话虽如此,但我认为在现实生活中没有人会那样做 ;)

您可以扩展阵列功能以满足您的要求。但不推荐。

只需在数组声明之前添加以下代码,您的代码就可以正常工作。

Array.prototype.replaceAll = function(oldString, updatedString){
    return this.slice().map((str)=>((str === oldString)?updatedString:str));
}