在JavaScript中使用filter()的4个实用案例
1、删除重复的值
const numbers = [3, 12, 54, 12, 4, 4, 3, 12, 16];
const filteredNumbers = numbers.filter((number, index) => numbers.indexOf(number) === index);
console.log(filteredNumbers); // [3, 12, 54, 4, 16]
2、删除无效值
const people = [
{ name: ‘Amy’, gender: ‘female’, age: ‘28’ },
{ name: ‘James’, gender: ‘male’, age: 13 },
{ name: ‘Victor’, gender: ‘male’, age: null },
{ name: ‘David’, gender: ‘male’, age: 28 },
{ name: ‘Simon’, gender: ‘male’, age: undefined },
{ name: ‘Anna’, gender: ‘female’, age: 21 },
{ name: ‘Jane’, gender: ‘female’, age: NaN }
];
const filteredPeople = people.filter(person => person.age !== undefined && typeof person.age === ‘number’ && !isNaN(person.age));
console.log(filteredPeople);
// [{ name: ‘James’, gender: ‘male’, age: 13 }, { name: ‘David’, gender: ‘male’, age: 28 }, { name: ‘Anna’, gender: ‘female’, age: 21 }]
3、过滤数字数组
const numbers = [23, 54, 1, 3, 72, 28];
const oddNumbers = numbers.filter(number => number % 2 !== 0);
console.log(oddNumbers); // [23, 1, 3]
const isPrime = number => {
if (number === 1) return false;
if (number === 2) return true;
for (let i = 2; i < number; i++) {
if (number % i === 0) return false;
}
return true;
}
const numbers = [23, 54, 1, 3, 72, 28];
const oddNumbers = numbers.filter(isPrime);
console.log(oddNumbers); // [23, 3]
4、过滤对象数组
const people = [
{ name: ‘Amy’, gender: ‘female’, age: 28 },
{ name: ‘James’, gender: ‘male’, age: 13 },
{ name: ‘Victor’, gender: ‘male’, age: 17 },
{ name: ‘David’, gender: ‘male’, age: 28 },
{ name: ‘Simon’, gender: ‘male’, age: 33 }
];
const filteredPeople = people.filter(person => person.age > 18);
console.log(filteredPeople);
// [{ name: ‘Amy’, gender: ‘female’, age: 28 }, { name: ‘David’, gender: ‘male’, age: 28 }, { name: ‘Simon’, gender: ‘male’, age: 33 }]
const people = [
{ name: ‘Amy’, gender: ‘female’, age: 28 },
{ name: ‘James’, gender: ‘male’, age: 13 },
{ name: ‘Victor’, gender: ‘male’, age: 17 },
{ name: ‘David’, gender: ‘male’, age: 28 },
{ name: ‘Simon’, gender: ‘male’, age: 33 }
];
const filteredPeople = people.filter(person => person.age > 18 && person.gender === ‘female’);
console.log(filteredPeople);
// [{ name: ‘Amy’, gender: ‘female’, age: 28 }]
结论
评论