使用splice方法(直接修改原数组)
let items = ["apple", "banana", "cherry", "date with almond", "elderberry", "fig"];
function removeItemsMatching(array, ...substrings) {
for (let i = array.length - 1; i >= 0; i--) {
for (let substring of substrings) {
if (array[i].includes(substring)) {
array.splice(i, 1);
break;
}
}
}
}
removeItemsMatching(items, "a", "date");
console.log(items);
使用filter方法(不修改原数组)
let items = ["apple", "banana", "cherry", "date with almond", "elderberry", "fig"];
function filterItemsNotMatching(array, ...substrings) {
return array.filter(item => !substrings.some(substring => item.includes(substring)));
}
let filteredItems = filterItemsNotMatching(items, "a", "date");
console.log(filteredItems);
console.log(items);