Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 26, 2022 06:44 am GMT

Array.every() - for checking if all items meet a condition

This article is the eighth of the Array Method Series. In this article, I will explain what the every Array method is.

What is the Every Method?

The every method of arrays is a higher-order function that asserts if ALL items in an array meet a certain condition. If all items meet it, it returns true, and if at least one item does not meet it, it returns false.

This method does not modify the array. It only loops through and applies a condition on each item until it finds the one that does not match. This means that if it finds an element that does not match, it doesn't continue looping through the remaining items in the array. It immediately returns false. And if it never finds such an item all through the loop, then it returns true

Syntax of the Every Method

array.every(function(item, index, array){  // condition to test item with  // return true or false})

The callbackFunction passed to the every method is applied to each item in the array until it finds the item that does not match the condition in the function.

The arguments passed to the callback function in each loop are the item, the index of the item, and the original array.

Without the Every Method

The every method is an abstracted function that does a quick check and stops at the first item that does not pass a certain criterion. Here's an example imitating the every method:

const array = [1, 2, 3, 4, 5, 6, 7, 8]let allNumbersLessThan5 = false;for (let i = 0; i < array.length; i++) {  const item = array[i]  console.log(item)  if (item >= 5) {    allNumbersLessThan5 = false    break  }}console.log(allNumbersLessThan5)// 1// 2// 3// 4// 5// false

This loop approach is similar to what the every method does in the background. It loops through each item, and when it finds the item that does not match the specified condition (that all numbers should be less than 5), it stops the loop and returns false.

From the logs, you would see that the loop stopped at 5, and since 5 did not pass the test, then the method already knows that NOT EVERY item meets the condition.

If it doesn't find a reason to stop the loop, then it means that all the numbers are less than 5.

With the Every Method

Here's how you achieve the previous result with every:

const array = [1, 2, 3, 4, 5, 6, 7, 8]const allNumbersLessThan5 = array.every(item => {  console.log(item)  return item < 5})console.log(allNumbersLessThan5)// 1// 2// 3// 4// 5// false

Easier to read with fewer lines of code. Let's see an example where everything passes:

const array = [1, 2, 3, 4, 5, 6, 7, 8]const allNumbersLessThan5 = array.every(item => {  return typeof item === "number"})console.log(allNumbersLessThan5)// true

Every item passes the condition, so indeed, EVERY item is a number.

The every method is useful when you have different values in an array and you want to assert that all of them meet a condition.


Original Link: https://dev.to/dillionmegida/arrayevery-for-checking-if-all-items-meet-a-condition-5h3p

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To