Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Remove All Characters of First String from Second in JavaScript



Let’s say, we have two strings that contains characters in no specific order. We are required to write a function that takes in these two strings and returns a modified version of the second string in which all the characters that were present in the first string are omitted.

Following are our strings −

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

Following is our function to remove all characters of first string from second −

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

Let's write the code for this function −

Example

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));

Output

The output in the console will be −

yt
Updated on: 2020-08-31T06:53:32+05:30

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements