1

I want something like this:

var string = "apple banana";
var array = ["apple", "banana", "orange", "grape"];
for(var i=0;i<array.length;i++){
  if(array[i] is found in (string) {
    remove the value;
  }
}

something like that. so basically, it would:

  • Declare a string and an array

  • Iterate through the array

  • If the array[i] value is present exactly in the string, it would remove it

  • The iteration should continue as normal

In case it still doesnt make sense:

String is "1 3 b c"

Array contains "1", "2","3", "a", "b", "c"

The array should now only contain "2" and "a".

1 Answer 1

2

.filter the array by whether the string does not .includes the substring being iterated over:

const doFilter = (str, arr) => arr.filter(substr => !str.includes(substr));

console.log(
  doFilter("apple banana", ["apple", "banana", "orange", "grape"]),
  doFilter("1 3 b c", ["1", "2","3", "a", "b", "c"]),
);

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

1 Comment

I think you need array.filter(substr => !string.includes(substr)) to remove the matching items.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.