ES6 必须要用的数组Filter() 方法,不要再自己循环遍历了!!!
生活随笔
收集整理的這篇文章主要介紹了
ES6 必须要用的数组Filter() 方法,不要再自己循环遍历了!!!
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1,來一個最簡單最常用的栗子:
獲得年齡為9歲的孩子
1 let arr = [2 {3 name:'小明',4 sex:0,5 age:96 },7 { 8 name:'小紅', 9 sex:1, 10 age:9 11 }, 12 { 13 name:'小亮', 14 sex:0, 15 age:10 16 } 17 ] 18 console.log(arr.filter(item => item.age === 9));結果為:[ { name: '小明' , sex: 0, age: 9 }, { name:'小紅' , sex: 1, age: 9 } ]
filter方法的返回值為符合過濾條件的元素;
2,去掉數(shù)組空字符串、undefined、null
1 let arr2 = [ 1,3,5,null,4,undefined,10,'hahah','','end' ]; 2 3 console.log(arr2.filter(item => item));結果為:[1, 3, 5, 4, 10, "hahah", "end"]
3,數(shù)組去重的用法
1 let arr3 = [ 1,3,3,6,5,5,8,9,8,'hahah','wer','hahah' ]; 2 console.log(arr3.filter((item,index,self) => self.indexOf(item) == index));轉(zhuǎn)載于:https://www.cnblogs.com/lml2017/p/10018572.html
總結
以上是生活随笔為你收集整理的ES6 必须要用的数组Filter() 方法,不要再自己循环遍历了!!!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【资源分享】CS起源 V34.4044(
- 下一篇: C#设置代码只在调试模式下执行