替换字符串中所有字符,但数组JavaScript中不存在的字符
假设我们必须编写一个函数-
replaceChar(str, arr, [char])
现在,用可选参数char替换字符串arr的数组中不存在的所有字符串str的字符。如果未提供char,则将其替换为'*'。
让我们为该函数编写代码。
完整的代码将是-
示例
const arr = ['a', 'e', 'i', 'o', 'u'];
const text = 'I looked for Mary and Samantha at the bus station.';
const replaceChar = (str, arr, char = '*') => {
const replacedString = str.split("").map(word => {
return arr.includes(word) ? word : char;
}).join("");
return replacedString;
};
console.log(replaceChar(text, arr));输出结果
此代码的控制台输出将是-
***oo*e***o***a***a****a*a***a*a****e**u****a*io**
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下