-1

I would like to find if my string contains any element of the array if it did I would like to remove it for now I could manage to find if it exist or not but not sure how to remove it here is a sample of my code

const arr = ["hi", "hello"," there"]
const msg = "hi I would like to eat ice";
console.log(arr.some(word => msg.toLowerCase().includes(word.toLowerCase()))); // result true
console.log(msg); // new message should be "I would like to eat ice"
0

1 Answer 1

1

You can do it like that but you need to take into account the left spaces (like the one after 'hi ').

const arr = ["hi", "hello"," there"];
const msg = "hi I would like to eat ice";
const newMsg = arr.reduce((acc, subStr) => acc.replace(subStr, ''), msg)
console.log(newMsg); // new message is " I would like to eat ice"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.