我想要这样的东西:
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;
}
}
类似的东西。所以基本上,它会:
声明一个字符串和一个数组
遍历数组
如果 array[i] 值恰好出现在字符串中,它将删除它
- 迭代应该照常进行
如果仍然没有意义:
字符串是“1 3 b c”
数组包含“1”、“2”、“3”、“a”、“b”、“c”
数组现在应该只包含“2”和“a”。
最佳答案
.filter
根据字符串是否.includes
被迭代的子字符串过滤数组:
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"]),
);
关于javascript - 如何查找数组值是否在字符串中并将其删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59097972/