On the JS in every () and some () method

. 1, some ():
      some () method for detecting elements in the array satisfies the specified conditions.

  • If there is an element in the array satisfies the condition, the expression returns true, the remaining elements will not perform detection.
  • If the array does not meet the conditions of the elements, false is returned.
var arr = [4, 5, 6, 7, 8, 9]; 
 
console.log( arr.some( function( item, index, array ){ 
    console.log( 'item=' + item + ',index='+index+',array='+array ); 
    return item > 6; 
}));

result:
Here Insert Picture Description

Note: some true if they are no longer carried out, will not be an empty array detection, does not change the original array

2、every():

      every () is run on each item in the array to a given function, the function returns true if for each, it returns true.

  • If the array has a detected element is not satisfied, then the whole expression evaluates to false, and the remaining elements will not be detected.
  • If all elements conditions are met, it returns true.
var arr = [4, 5, 6, 7, 8, 9]; 
 
console.log( arr.every( function( item, index, array ){ 
    console.log( 'item=' + item + ',index='+index+',array='+array ); 
    return item > 6; 
}));

result:
Here Insert Picture Description

Note: every encounter false if it is no longer carried out, will not be an empty array detection, does not change the original array

Published 43 original articles · won praise 149 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_42881768/article/details/104690802