This is the 21st day of my participation in Gwen Challenge

preface

In the process of front-end development, the iterative processing of array is also a common operation. There are two commonly used iteration methods in JavaScript arrays: every() and some(), both of which iterate over arrays in JS. So let’s compare the use of every() and some() in detail.

First, every ()

1.1, every grammar

Arr. Every (callback(element[, index[, array]])[, thisArg]);

Parameter Description:

1.2, every definition

The every() method is used to check whether all elements in an array pass a test for a given function. It returns a bool.

Note: This method returns true in all cases if an empty array is received. Return true if each return from the callback function is truthy, false otherwise.

1.3

Every () executes the callback function once for each element in the array until it finds an element that causes the callback to return Falsy. If one is found, every() will immediately return false. Otherwise callback returns true for each element, and every returns true. Callback will only be called for indexes that have been assigned, not indexes that have been deleted or have never been assigned.

1.4, The use of every

The simple use of every() is as follows:

Eg1: The element that checks if all elements in the array are less than 100

const isBelow = (currentValue) => currentValue < 100; Const array1 = [10, 30, 39, 99, 10, 13]; console.log(array1.every(isBelow)); // The final output is: trueCopy the code

Eg2: Checks whether all elements in the array are greater than 100

function isBigEnough(element, index, array) {
  return element >= 100;
}
 
[12, 5, 8, 130, 44].every(isBigEnough);   // false
 
[120, 540, 180, 130, 440].every(isBigEnough); // true
Copy the code

Eg3: Based on an example of eg2, rewritten to use arrow functions

[12, 5, 8, 130, 44].every(x => x >= 100); // false
[120, 540, 180, 130, 440].every(x => x >= 100); // true
Copy the code

Second, some ()

2.1, some syntax

Arr. Some (callback(Element [, index[, array]])[, thisArg]);

Parameter Description:

2.2. Some definitions

The some() method tests that at least one element in the array passes the provided function test. It returns a Boolean value.

Note: If you test with an empty array, it returns false in any case. Returns true if at least one element in the array passes the callback test; The return value is false only if all elements fail the callback test.

2.3, some description

Some () performs a callback function once for each element in the array until a value is found that causes the callback to return a Boolean value that can be converted to true. If such a value is found, some() will immediately return true. Otherwise some() returns false. Callback will only be called on indexes that have values, not indexes that have been deleted or have never been assigned a value.

2.4. Use some

A simple use of some() is as follows:

Eg1: Checks if an element in the array is even

const array = [1, 2, 3, 4, 5]; const even = (element) => element % 2 === 0; // Check if an element is even console.log(array.some(even)); // The final output is: trueCopy the code

Eg2: Checks if an element in the array is greater than 100

function isBiggerThan10(element, index, array) {
  return element > 100;
}
[20, 50, 80, 10, 40].some(isBiggerThan10);  // false
[120, 500, 800, 10, 40].some(isBiggerThan10); // true
Copy the code

Eg3: Checks to see if an element in an array has a value

var vegetables = ['cabbage', 'cucumber', 'eggplant', 'potato'];
function checkAvailability(arr, val) {
  return arr.some(function(arrVal) {
    return val === arrVal;
  });
}
checkAvailability(vegetables, 'tomato');   // false
checkAvailability(vegetables, 'eggplant'); // true
Copy the code

Eg4: The arrow function applies to some(), using eg2 as an example

[20, 50, 80, 10, 40].some(x => x > 100);  // false
 
[120, 500, 800, 10, 40].some(x => x > 100); // true
Copy the code

Every () and some()

3.1 Similarities

The functions every() and some() are used to iterate over an array in JS to check whether all elements in the array match the test of the specified function, and to check all elements in the array.

3.2 Differences

Every () is a test that checks that all elements in an array satisfy a given function. Some () checks all elements in the array and returns true as long as one element is satisfied, or false if all elements are not satisfied.

Some () returns true if the array contains a valid value and is found immediately. The rest of the element is not searched. Every () starts by looking for an element in the array that meets the criteria. Every element in the array that does not meet the criteria returns false, and the rest of the elements are not searched.

Four, the last

Use every() and some() based on actual conditions and business requirements. The above is all the content of this chapter. Welcome to pay attention to the wechat public account of Sanzhan “Program Ape by Sanzhan”, and the Sina Weibo account of Sanzhan “Sanzhan 666”, welcome to pay attention!