Summary of js array search method

1,indexOf()

let arr = [1, 2, 3, 4, 5, 6];
arr.indexOf(7)  //-1
arr.indexOf(6)  //5

Returns the subscript if the string value to be retrieved appears, returns -1 if it does not appear

2,includes()

let arr = [1, 2, 3, 4, 5, 6];
arr.includes(7)  //false
arr.includes(6)  //true

Whether to include a specified value, if it is true, otherwise false

3,filter()

let arr = [1, 2, 3, 4, 5, 6];
let find=arr.filter(function(item){
    
    
	return item%2===0
})
console.log(find)	//[2, 4, 6]

The filter() method creates a new array. The elements in the new array are checked for all elements in the specified array that meet the conditions. The empty array is not detected, and the original array is not changed.

4,find()、findIndex()

let arr = [1, 2, 3, 4, 5, 6];
let find1=arr.find(function(item){
    
    
	return item==3
})
console.log(find1)	//3
let find2=arr.findIndex(function(item){
    
    
	return item==3
})
console.log(find2)	//2

find() until it finds the first member whose return value is true, and then returns that member. If there is no qualified member, undefined is returned, and findIndex() returns the following table of the member

Guess you like

Origin blog.csdn.net/eightNine1102/article/details/106102724