JS object-array processing method every method

every() method

1. Used to detect whether all elements of the array meet the specified conditions (provided by the function)/2,

2. If one element in the array does not meet the condition, the entire expression returns false, and the rest will not be checked again. If all the conditions are met, it returns true.

note

1. The every() method does not detect empty arrays and does not change the original array

grammar

array.every(function(currentValue,index,arr), thisValue)
parameter description
function(currentValue,index,arr)

have to. Function, each element in the array will execute the entire function

parameter description
currentValue have to. The index value of the current element
index Optional. The index value of the current element
arr Optional. The array object to which the current element belongs

 

thisValue Optional, the object is used when the callback is executed, passed to the function, and used as the value of'this'. If thisValue is omitted, the value of'this' is'undefined'

 

 

 

 

 

 

 

 

 

Practical application (1)

var ages = [32, 33, 16, 40];

ages.every(checkAdult)

function checkAdult(age) {
    return age >= 18;
}

Practical application (two)

//判断数组是否连续
 let ressss = a1rr.every((cur, ind, arr) => {

                if(ind == arr.length-1) return true
                return arr[ind + 1] && arr[ind + 1] - cur == 1
            
  });
  console.log(ressss)

Novice, welcome to discuss.

Guess you like

Origin blog.csdn.net/qq_40010841/article/details/113942519