Home > Web Front-end > JS Tutorial > body text

Summary of es6 filter() array filtering method (with code)

不言
Release: 2019-04-03 10:15:30
forward
4107 people have browsed it

This article brings you a summary of the es6 filter() array filtering method (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Array.every(x=>x) must satisfy each one.

Array.some(x=>x) must satisfy one of them.

Array.find(findIndex), returns the first value that meets the conditions.

Array.filter (filter into a new array)

Array methods are divided into two categories

1) Change the original array

push, pop, shift, unshift, sort, reverse, splice

2) Do not change the original array concat, join-->

split, toStringpush: add data starting from the last bit of the array

pop: Cut the last digit of the array

shift: Cut the first digit of the array

unshift: Add the number to the first digit of the array

reverse: Put Reverse the original array

splice:arr.splice(start from the number, intercept the length, add new data at the cut)

concat: connect join: return string

slice: Intercept arr.slice (intercept from this to this)

es6 filter() Summary of array filtering method
1. Create an array and determine whether a certain item exists in the array Value

var newarr = [
  { num: 1, val: 'ceshi', flag: 'aa' },
  { num: 2, val: 'ceshi2', flag: 'aa2'  }
]
console.log(newarr.filter(item => item.num===2 ))
Copy after login

2. You can also use the above method to filter out those with num of 2 and leave those with num of 1

var newarr = [
  { num: 1, val: 'ceshi', flag: 'aa' },
  { num: 2, val: 'ceshi2', flag: 'aa2'  }
]
console.log(newarr.filter(item => item.num!=2 ))
Copy after login

3. Remove empty arrays, empty strings, undefined, and null

var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter(item => item)
console.log(newArr)
Copy after login
var arr = ['1','2',null, '3.jpg',null]
var newArr = arr.filter(item => item)
console.log(newArr)
Copy after login
//空字符串里面不能包含空格
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)
Copy after login

4. Remove non-conforming items from the array

var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)  
console.log(newArr)
Copy after login

5. Filter non-conforming items

var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf(&#39;2&#39;)<0) 
console.log(newArr)
Copy after login

6. Remove duplicates from the array

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]
Copy after login

[Related recommendations: JavaScript video tutorial]

The above is the detailed content of Summary of es6 filter() array filtering method (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template