1.建立乙個陣列,判斷陣列中是否存在某個值
var newarr = [2.也可以通過上面方法過濾掉num為2的留下num為1的, ]console.log(newarr.filter((item,index,array) => item.num===2 ))
var newarr = [3.去掉空陣列空字串、undefined、null, ]console.log(newarr.filter((item,index,array) => item.num!=2 ))
var newarr = arr.filter((item,index,array) => item) console.log(newarr) var arr = ['1','2',null, '3.jpg',null] var newarr = arr.filter(item => item) console.log(newarr) >//空字串裡面不能包含空格 var arr = ['1','2','', '3.jpg',''] var newarr = arr.filter(item => item) console.log(newarr)
4.去掉陣列中不符合項
var arr = [20,30,50, 96,50]5.過濾不符合項var newarr = arr.filter((item,index,array) => item>40) console.log(newarr)
var arr = ['10','12','23','44','42']var newarr = arr.filter(
(item,index,array)
=> item.indexof('2')<0) console.log(newarr)6.陣列去重
var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];var arr2 = arr.filter((x, index,self)=>self.indexof(x)===index)
console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]
es6 filter 陣列過濾方法總結
1.建立乙個陣列,判斷陣列中是否存在某個值 var newarr console.log newarr.filter item item.num 2 2.也可以通過上面方法過濾掉num為2的留下num為1的 var newarr console.log newarr.filter item item...
es6 filter 陣列過濾方法總結
1.建立乙個陣列,判斷陣列中是否存在某個值 var newarr console.log newarr.filter item item.num 2 2.也可以通過上面方法過濾掉num為2的留下num為1的 var newarr console.log newarr.filter item item...
es6 filter 陣列過濾方法總結
1.建立乙個陣列,判斷陣列中是否存在某個值 var newarr console.log newarr.filter item item.num 2 2.也可以通過上面方法過濾掉num為2的留下num為1的 var newarr console.log newarr.filter item item...