JS数组的filter()方法基本用法是什么,有什么要注意
Admin 2022-09-16 群英技术资讯 582 次浏览
filter()创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
array.filter(function(currentValue,index,arr), thisValue);
返回
filter() 方法用于把Array中的某些元素过滤掉,然后返回剩下的未被过滤掉的元素。
1、filter() 不会对空数组进行检测;
2、filter() 不会改变原始数组。
1.返回数组array中所有元素都大于等于14的元素、返回等于14、返回大于某个值和小于某个值的元素的元素。
const array = [14, 17, 18, 32, 33, 16, 40]; const newArr = array.filter(num => num > 14) console.log(newArr);//打印 [17,18,32,33,16,40] // 查找某个值------------------------- const array = [14, 17, 18, 32, 33, 16, 40]; const newArr = array.filter(num => num == 14) console.log(newArr);//打印 [14] //返回大于某个值和小于某个值的元素 const array = [14, 17, 18, 32, 33, 16, 40]; const newArr = array.filter(num => num > 14 && num < 33) console.log(newArr);//打印 [17, 18, 32, 16]
2.数组去重操作:对数组array中所有相同的元素进行去重复操作。
const array = [2, 2, 'a', 'a', true, true, 15, 17] const newArr = array.filter((item, i, arr) => { return arr.indexOf(item) === i }) console.log(newArr);//打印 [2, 'a', true, 15, 17] //------------------------------------------------------------------------- const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 4, 5, 6, 7, 9,] const newArr = array.filter((item, i, arr) => { return arr.indexOf(item) === i }) console.log(newArr);// 打印 [1, 2, 3, 4, 5, 6, 7, 8, 9]
3、数组中保留奇数或者偶数。
//保留偶数---------------------------------------- const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const newArr = array.filter((item, i, arr) => { return item % 2 === 0 }) console.log(newArr);// 打印 [2, 4, 6, 8, 10] //保留奇数---------------------------------------- const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const newArr = array.filter((item, i, arr) => { return item % 2 !== 0 }) console.log(newArr);// 打印 [1, 3, 5, 7, 9]
4、去掉数组中的假值,比如:空字符串、undefined、null、0、false。
const array = [ { id: 3 }, { id: 4 }, { id: null }, { id: undefined }, { id: '' }, { id: 0 }, { id: false } ] const newArr = array.filter(({ id }) => id) console.log(newArr);// 打印 [{ "id": 3 },{ "id": 4 }] //------------------------------------------------------------------- const array = [undefined, null, 3, 5, 'a', false, 0] const newArr = array.filter(item => item) console.log(newArr);// 打印 [3, 5, 'a']
5、把对象数组array中的某个属性值取出来存到数组newArr中。
const array = [ { name: "a", type: "letter" }, { name: '1', type: "digital" }, { name: 'c', type: "letter" }, { name: '2', type: "digital" }, ]; const newArr = array.filter((item, i, arr) => { return item.type === "letter" }) console.log(newArr); // 打印 [{ "name": "a", "type": "letter" }, { "name": "c", "type":"letter" }]
6、filter结合find方法,实现两个数组的补集的解决方法,oldArr的元素newArr中都有,在newArr中去掉所有的oldArr。
find() 方法返回数组中满足提供的测试函数的第一个元素的值。这里有四个元素,那么就会返回两个数组元素相等的值,这里取反就返回不相等的值, 不取反的时候因为30的元素不符合,所以不返回30的值。
const array = [32, 4, 11, 55, 46, 99, 104, 54, 16, 33, 78, 43, 40] const oldArr = [32, 33, 16, 40, 30] function myfunction() { const result = array.filter(item1 => { //此处取反去掉,将变换元素状态 return !oldArr.find(item2 => { return item1 === item2 }) }) return result } const newArr = myfunction() console.log(newArr); // 取反打印 [4, 11, 55, 46, 99, 104, 54, 78, 43] // 不取反打印 [32, 16, 33, 40] 此处30的元素不符合,所以不返回30的值
根据单个名字或者单个年龄筛选,用filter方法,判断条件之间是或的关系。
// 根据名字或者年龄筛选 function filterByName2(aim, name, age) { return aim.filter(item => item.name == name || item.age == age) } console.log(filterByName2(aim,'Leila',19))
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了js实现列表自动滚动循环播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Element-ui表单校验规则,使得错误提示可以直接在form-item下面显示,无需弹出框,感兴趣的小伙伴们可以参考一下
jQuery中怎样设置所有input失效?在实际的项目中,有时候我们血药设置文本框不可用,那么怎样才让全部的input失效呢?文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
在我们做项目中,常见的是横向表格,但是偶尔的需求,也会做竖向的表格。类似于这样的效果图,其实并不一定非得使用UI组件,有的时候使用原生的方式去做
let声明的范围是块作用域,var声明的范围是函数作用域。let和var的另一个重要区别是,let声明的变量不会在作用区域内得到改善。let声明前的执行瞬间称为暂时性死区。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008