从第二个 JavaScript 中删除第一个字符串的所有字符

javascriptweb developmentobject oriented programming更新于 2025/10/24 22:36:17

假设我们有两个字符串,其中包含无特定顺序的字符。我们需要编写一个函数,该函数接收这两个字符串并返回第二个字符串的修改版本,其中省略了第一个字符串中存在的所有字符。

以下是我们的字符串 −

const first = "hello world";
const second = "hey there";

以下是我们从第二个字符串中删除第一个字符串的所有字符的函数 −

const removeAll = (first, second) => {
   const newArr = second.split("").filter(el => {
      return !first.includes(el);
   });
   return newArr.join("");
};

让我们来编写该函数的代码 −

示例

const first = "hello world";
const second = "hey there";
const removeAll = (first, second) => {
   const newArr = second.split("").filter(el => {
      return !first.includes(el);
   });
   return newArr.join("");
};
console.log(removeAll(first, second));

输出

控制台中的输出将是 −

yt

相关文章


有用资源