If there is an array in js, how to determine whether an element exists in the array?

Method 1: Can be judged by the for loop:

Var arr=[“apple”,”banana”,”orange”]// Array to match
Var value=”orange”// Element to match
function isInArray(arr,value){
for(var i = 0; i < arr.length; i++){
// console.log(value)
if(value === arr[i]){
Console. log(” matches value, which is the “+ I +” of the value)
return true;
}
}
return false;
}
IsInArray (arr,value)// Execute statement

IndexOf () returns the index value of the element in the array if it exists, or -1 if it does not:

var site = [‘runoob’, ‘google’, ‘taobao’,’google’];
console.log(site.indexOf(‘google’))

So we can make a judgment based on the value that we put back

 var index=site.indexOf(‘google’)
if(index<-1){
Console. log(” Not in array “)
}


Method 3: find() finds the first array element that matches the criteria. Its argument is a callback function that the array elements iterate through until they find the first element that returns true, then return that element, or undefined otherwise.

Var DDD = arr.find(function(val,index,arr) {//val: the selected element,index: the index value of the current element,arr: the array object of the current element
return val === “taobao”
})
console.log(ddd)

Tip: find() does not execute for empty arrays

FindIndex () returns the first matching value in the array, or -1 if there is none

Array.findindex () is similar to array.find() in that it returns the location of the first eligible array element, or -1 if all elements fail.

  let sd=arr.findIndex(function(val,index,arr){
Return val === “googlccDDddce “// find the first matching value in the array, if not return -1
})
console.log(sd)

Method 5: filter() creates a new array by checking all the elements in the specified array

function checkAdult(age) {

return age == “runoob”;
}
console.log(arr.filter(checkAdult))

ForEach ()// forEach(),for in(),for of() are the same as for()

Var arr=[“apple”,”banana”,”orange”]// Array to match
var value=”orange”
arr.forEach((item, index) => {
if(item==”orange”){
console.log(item,index);
}
})

The arr.include() method is used to determine whether an array contains a specified value, returning true if it does, false otherwise

Var arr=[“apple”,”banana”,”orange”]// Array to match
var value=”orange”
console.log(arr.include(value))

Method 8: The some() method is used to check whether an element in an array satisfies a specified condition (provided by the function). The some() method executes each element of the array in turn: if one element meets the criteria, the expression returns true, and the remaining elements are not checked. If no element satisfies the condition, false is returned.

var ages = [3, 10, 18, 20];

var pp=ages.some(function(age){ return age == 18; })

console.log(pp)

The $.inarray () function is used in jq to find the specified value in the array and return its index value (-1 if no value is found). The source array is not affected, and the filtering result is reflected only in the returned result array.

Var arr=[“apple”,”banana”,”orange”]// Array to match

 console.log($.inArray( “orange”, arr ))

IndexOf (), some(), indexOf (), indexOf ()