使用 Filtered String 和 Remove from Original Array 过滤字符串数组
Filter an Array of strings using Filtered String and Remove from Original Array
我想从包含单词 Evil (filterString) 的数组中删除一些元素。
let guests = ["Partner", "Evil Nice Relative 1", "Nice Relative 2", "Evil One", "another evil", "another one", "another evil is here", "strange Evil is here", "someone Nicer", "Ugly Evil Bad"];
const filteredArray = [];
const filterString = "Evil";
function checkEvil() {
guests.filter((element, index) => {
if (element.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
console.log(index);
guests.splice(index,1);
} else {
filteredArray.push(element);
}
});
console.log(guests);
}
这是我得到的原始数组 (guests):
['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer']
只希望在所需字符串 (Evil) 被过滤后更新 guests 数组。
既然你想改变原始数组,那么你可以这样做:
let guests = [
"Partner",
"Evil Nice Relative 1",
"Nice Relative 2",
"Evil One",
"another evil",
"another one",
"another evil is here",
"strange Evil is here",
"someone Nicer",
"Ugly Evil Bad",
];
const filterString = "Evil";
function checkEvil() {
for (let i = guests.length - 1; i >= 0; i--) {
const element = guests[i];
if (element.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
guests.splice(i, 1);
}
}
console.log(guests);
}
checkEvil();
1) 您可以使用 filter
and match
轻松获得结果:
const arr = [
"Partner",
"Nice Relative 2",
"another evil",
"another one",
"strange Evil is here",
"someone Nicer",
];
const result = arr.filter((s) => !s.match(/evil/i));
console.log(result);
2) 您也可以使用 forEach 并匹配为:
let guests = [
"Partner",
"Evil Nice Relative 1",
"Nice Relative 2",
"Evil One",
"another evil",
"another one",
"another evil is here",
"strange Evil is here",
"someone Nicer",
"Ugly Evil Bad",
];
const filteredArray = [];
const filterString = "Evil";
function checkEvil() {
guests.forEach(element => {
if (!element.match(/evil/i)) filteredArray.push(element);
});
}
checkEvil();
console.log(filteredArray);
<script>
var list = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
var new_list = [];
for(var i=0; i<list.length; i++) {
if(!list[i].toLowerCase().includes('evil')) {
new_list.push(list[i]);
}
}
console.log(new_list);
</script>
定义一个模式然后按它过滤
var arr = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
const PATTERN = 'EVIL';
arr = arr.filter(str => str.toUpperCase().indexOf(PATTERN) === -1);
您可以过滤每个值不包含 'evil'
单词的数组:
const arr = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
const filteredArr = arr.filter(val=>!val.toLowerCase().includes('evil'))
console.log(filteredArr)
我想从包含单词 Evil (filterString) 的数组中删除一些元素。
let guests = ["Partner", "Evil Nice Relative 1", "Nice Relative 2", "Evil One", "another evil", "another one", "another evil is here", "strange Evil is here", "someone Nicer", "Ugly Evil Bad"];
const filteredArray = [];
const filterString = "Evil";
function checkEvil() {
guests.filter((element, index) => {
if (element.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
console.log(index);
guests.splice(index,1);
} else {
filteredArray.push(element);
}
});
console.log(guests);
}
这是我得到的原始数组 (guests):
['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer']
只希望在所需字符串 (Evil) 被过滤后更新 guests 数组。
既然你想改变原始数组,那么你可以这样做:
let guests = [
"Partner",
"Evil Nice Relative 1",
"Nice Relative 2",
"Evil One",
"another evil",
"another one",
"another evil is here",
"strange Evil is here",
"someone Nicer",
"Ugly Evil Bad",
];
const filterString = "Evil";
function checkEvil() {
for (let i = guests.length - 1; i >= 0; i--) {
const element = guests[i];
if (element.toLowerCase().indexOf(filterString.toLowerCase()) !== -1) {
guests.splice(i, 1);
}
}
console.log(guests);
}
checkEvil();
1) 您可以使用 filter
and match
轻松获得结果:
const arr = [
"Partner",
"Nice Relative 2",
"another evil",
"another one",
"strange Evil is here",
"someone Nicer",
];
const result = arr.filter((s) => !s.match(/evil/i));
console.log(result);
2) 您也可以使用 forEach 并匹配为:
let guests = [
"Partner",
"Evil Nice Relative 1",
"Nice Relative 2",
"Evil One",
"another evil",
"another one",
"another evil is here",
"strange Evil is here",
"someone Nicer",
"Ugly Evil Bad",
];
const filteredArray = [];
const filterString = "Evil";
function checkEvil() {
guests.forEach(element => {
if (!element.match(/evil/i)) filteredArray.push(element);
});
}
checkEvil();
console.log(filteredArray);
<script>
var list = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
var new_list = [];
for(var i=0; i<list.length; i++) {
if(!list[i].toLowerCase().includes('evil')) {
new_list.push(list[i]);
}
}
console.log(new_list);
</script>
定义一个模式然后按它过滤
var arr = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
const PATTERN = 'EVIL';
arr = arr.filter(str => str.toUpperCase().indexOf(PATTERN) === -1);
您可以过滤每个值不包含 'evil'
单词的数组:
const arr = ['Partner', 'Nice Relative 2', 'another evil', 'another one', 'strange Evil is here', 'someone Nicer'];
const filteredArr = arr.filter(val=>!val.toLowerCase().includes('evil'))
console.log(filteredArr)