Js determines whether an array contains a specified element

Method 1: arr.indexOf (an element): if not found, return -1.
 if(arr.indexOf(某元素) > -1){
    
    //则包含该元素}

example:

 var fruits = ["Banana", "Orange", "Apple", "Mango"];
 var a = fruits.indexOf("Apple");  // 2
 //以上输出结果意味着 "Apple" 元素位于数组中下标为 2 的位置。

indexOf() complete syntax:
 array.indexOf(item,start)

 //参数:  item:必须。查找的元素。  start:可选的整数参数。规定在字符串中开始检索的位置。  它的合法取值是 0 到 stringObject.length - 1。  如省略该参数,则将从字符串的首字符开始检索。
example:

 var fruits=["Banana","Orange","Apple","Mango","Banana","Orange","Apple"];
 var a = fruits.indexOf("Apple",4);  // 6

Note: string.indexOf() returns the position of the first occurrence of a specified string value in the string.

  1. This method will retrieve the string stringObject from beginning to end to see if it contains the substring searchvalue. The search starts at the fromindex of the string or the beginning of the string (when fromindex is not specified). If a searchvalue is found, the position of the first occurrence of searchvalue is returned.
  2. Character positions in stringObject are 0-based.
  3. To find the last occurrence of a string, use the lastIndexOf() method.
Method 2: arr.find()

The find() of an array instance is used to find the first array element that meets the criteria. Its parameter is a callback function, and all array elements traverse the callback function in turn until the first element with a return value of true is found, and then returns the element, otherwise returns undefined.
The find() method calls the function execution once for each element in the array:

  • When the element in the array returns true when the condition is tested, find() returns the element that meets the condition, and the value after that will not call the execution function.
  • Returns undefined if there are no eligible elements
  • Note: The find() function will not execute for an empty array.
  • Note: find() does not change the original value of the array.
 [1, 5, 10, 15].find(function(value, index, arr) {
    
    
     return value > 9;
 }) // 10
 /**
 *使用方法
 */
 arr.find(function(value) {
    
    
     if(value === 要查找的值) {
    
    
         //则包含该元素
     }
 })
Method 3: array.findIndex()

array.findIndex() is very similar to array.find(), returning the position of the first array element that meets the conditions, or -1 if all elements do not meet the conditions.
The findIndex() method calls the function once for each element in the array:
when the element in the array returns true when the test condition is met, findIndex() returns the index position of the element that meets the condition, and the subsequent value will not be called again function. If there is no element that meets the conditions, return -1 Note: findIndex() function will not be executed for an empty array.
Note: findIndex() does not change the original value of the array

[1,5,10,15].findIndex(function(value, index, arr) {
    
    
     return value > 9;
 }) // 2

Explanation:
 Method 2 and Method 3, both methods can find NaN, making up for the deficiency of Method 1 IndexOf().

 [NaN].indexOf(NaN) 
 // -1
 
 [NaN].findIndex(y => Object.is(NaN, y))
 // 0
Method 4: for()

Traverse the array, then if judge

 var arr = [1, 5, 10, 15];
 //传统for
 for(let i=0; i<arr.length; i++) {
    
    
     if(arr[i] === 查找值) {
    
    
         //则包含该元素
     }
 }
 // for...of
 for(v of arr) {
    
    
     if(v === 查找值) {
    
    
         //则包含该元素
     }
 }
 //forEach
 arr.forEach(v=>{
    
    
     if(v === 查找值) {
    
    
         //则包含该元素
     }
 })

Guess you like

Origin blog.csdn.net/qq_42697806/article/details/125423482