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:
IndexOf () returns the index value of the element in the array if it exists, or -1 if it does not:
So we can make a judgment based on the value that we put back
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.
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.
Method 5: filter() creates a new array by checking all the elements in the specified array
function checkAdult(age) {
ForEach ()// forEach(),for in(),for of() are the same as for()
The arr.include() method is used to determine whether an array contains a specified value, returning true if it does, false otherwise
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 ()