一、为什么要使用array.fifler()
因为它简单,好用,清晰,可拓展性强,而且比for、foreach还有非常不常用的while、do...while高级,代码清晰,可读性强,代码就看起来很优雅,如果都是嵌套循环和嵌套回调,看起来就是一团乱麻,可读性差,很不优雅。
要做优雅的程序员,写优雅的代码。
array.fifler()方法就像名字一样,他就是一个过滤器,比较语义化,上手较快。
二、array.fifler()的使用与技巧
2.1、基本语法
array.filter(callback(element, index, array), thisarg)
其中callback回调函数对每个数组元素执行的函数,接受三个参数:
- element:当前遍历到的元素
- index (可选):当前遍历到的索引
- array (可选):调用 filter 的数组本身
thisarg是执行 callback 时用作 this 的值。
2.2、返回值
一个新的数组,包含通过测试的元素。
2.3、使用技巧
综上所述,array.fifler()就是一个数组的过滤器,同时不影响数组本身的样子,返回的是一个新的数组,常用于对基础数据进行筛选,以适用于特定的情况。
应用场景:数据筛选、数据清洗和链式调用。
2.3.1、筛选数字数组中的偶数
最基础的例子,基于原始数据numbers数组,通过array.fifler()生成一个只含偶数的新数组evennumbers。
// 示例1:筛选数组中的偶数 const numbers = [1, 2, 3, 4, 5, 6]; const evennumbers = numbers.filter(number => number % 2 === 0); console.log(evennumbers); // [2, 4, 6]
2.3.2、数据筛选:筛选出高价值客户
假设有一个客户消费记录的数组,我们想要筛选出过去一年内消费总额超过10000元且订单数量超过5个的高价值客户。
// 示例2:筛选出高价值客户 const customers = [ { id: 1, name: 'alice', orders: [ { amount: 1200, date: '2023-05-15' }, { amount: 2500, date: '2023-07-22' }, { amount: 1800, date: '2023-08-05' } ]}, { id: 2, name: 'bob', orders: [ { amount: 9000, date: '2023-03-01' }, { amount: 2200, date: '2023-09-12' } ]}, { id: 3, name: 'charlie', orders: [ { amount: 750, date: '2023-02-17' }, { amount: 1100, date: '2023-04-03' }, { amount: 1500, date: '2023-05-09' }, { amount: 1300, date: '2023-06-21' } ]}, { id: 4, name: 'david', orders: [ { amount: 2000, date: '2023-01-05' }, { amount: 1700, date: '2023-02-20' }, { amount: 2300, date: '2023-03-18' } ]}, { id: 5, name: 'eve', orders: [ { amount: 3500, date: '2023-04-08' }, { amount: 4200, date: '2023-05-22' } ]}, { id: 6, name: 'frank', orders: [ { amount: 550, date: '2023-03-02' }, { amount: 850, date: '2023-08-16' } ]}, // ... 更多客户 ]; const highvaluecustomers = customers.filter(customer => { const totalspent = customer.orders.reduce((sum, order) => { const orderdate = new date(order.date); const oneyearago = new date(); oneyearago.setfullyear(oneyearago.getfullyear() - 1); return sum + (orderdate >= oneyearago ? order.amount : 0); }, 0); return totalspent > 10000 && customer.orders.length > 5; }); console.log(highvaluecustomers);
2.3.3、数据清洗:移除无效的用户记录
假设我们有一个包含用户注册信息的数组,我们想要移除那些邮箱地址无效或密码长度不符合要求的用户记录。
// 示例3:移除无效的用户记录 const users = [ { id: 1, name: 'alice', email: 'alice', password: 'alice123' }, { id: 2, name: 'bob', email: 'bob@example.com', password: 'b' }, { id: 3, name: 'charlie', email: 'charlie@work.com', password: 'ch@rl1e!' }, { id: 4, name: 'diana', email: 'diana', password: 'd123456' }, { id: 5, name: 'edward', email: 'edward@', password: 'edwardpassword' }, { id: 6, name: 'fiona', email: 'fiona123', password: 'fionap@ss' }, { id: 7, name: 'george', email: 'george@example.com', password: 'g' }, { id: 8, name: 'hannah', email: 'hannah@hann.com', password: 'hannahpass123!' }, { id: 9, name: 'ivan', email: 'ivan', password: 'ivanivanivan' }, { id: 10, name: 'julia', email: 'julia@julia', password: 'ju@123' }, // ... 更多用户 ]; const validusers = users.filter(user => { // 使用正则表达式验证邮箱格式 const emailregex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // 密码长度至少为6个字符 return emailregex.test(user.email) && user.password.length >= 6; }); console.log(validusers);
2.3.4、链式调用:计算员工的平均薪资增长
假设我们有一个员工薪资记录的数组,我们想要找出过去两年内薪资增长超过10%的员工,并且计算他们的平均薪资增长百分比。
// 示例4:计算员工的平均薪资增长 const employees = [ { id: 1, name: 'alice', salary: 5000, salarytwoyearsago: 4000 }, { id: 2, name: 'bob', salary: 6500, salarytwoyearsago: 7000 }, { id: 3, name: 'charlie', salary: 8000, salarytwoyearsago: 6500 }, { id: 4, name: 'david', salary: 7200, salarytwoyearsago: 6000 }, { id: 5, name: 'eve', salary: 9500, salarytwoyearsago: 8200 }, { id: 6, name: 'frank', salary: 6800, salarytwoyearsago: 5800 }, { id: 7, name: 'grace', salary: 7800, salarytwoyearsago: 7200 }, { id: 8, name: 'heidi', salary: 9200, salarytwoyearsago: 8500 }, { id: 9, name: 'ivan', salary: 6300, salarytwoyearsago: 5500 }, { id: 10, name: 'judy', salary: 8600, salarytwoyearsago: 7800 }, // ... 更多员工 ]; const averagesalarygrowth = employees .filter(employee => { const growth = (employee.salary - employee.salarytwoyearsago) / employee.salarytwoyearsago; return growth > 0.10; }) .map(employee => { const growth = (employee.salary - employee.salarytwoyearsago) / employee.salarytwoyearsago; return growth * 100; // 转换为百分比 }) .reduce((totalgrowth, growth) => totalgrowth + growth, 0) / employees.length; console.log(`the average salary growth over the past two years is: ${averagesalarygrowth.tofixed(2)}%`);
三、总结
用array.filter()来实现数据筛选、数据清洗和链式调用,相对于for循环更加清晰,语义化强,能显著提升代码的可读性和可维护性。
到此这篇关于javascript中通过array.filter()实现数组的数据筛选、数据清洗和链式调用(js数组过滤器的使用示例)的文章就介绍到这了,更多相关js array.filter()数组数据筛选内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论