DEV Community

Nedy Udombat
Nedy Udombat

Posted on

Understanding Javascript Array Series IX - Array Loops & Iteration Part VI

In the previous article, I talked about iterating over arrays using the forEach array method. You can check it out below:

Today, I will talk about using array.some() to iterate over arrays.

Some()

This method basically checks that at least one element in an array passes the specified test and returns true to that effect. If you have an array [4, 6, 7, 8, 9] and you are asked to check if any of the elements is a prime number. At a glance we can see that the array contains the number 7 which is a prime number, so we would say yes the array contains a prime number. Similarly Array.some() works in the same manner, let's see it in action below:

const array = [4, 6, 7, 8, 9];
const isPrimeNumber = num => {
  for(let i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num > 1;
}

console.log(array.some(isPrimeNumber)); // true

This loop runs until the test case passes. The prime number function was gotten from StackOverflow.

Let's take a look at the syntax below:

   // syntax
   arr.some(([currentValue], [arrayIndex], [arr]) => {
     // [specified condition]
   });

   or

   arr.some(callback([currentValue], [arrayIndex], [arr]));

[currentValue]: This is the current item in the array that is being processed. After the procession, the current value becomes the value of the next element in the array.

[arrayIndex]: This is the index of the current value in the array. This also changes after the current value has been processed.

[arr]: This is the array being iterated over.

[callback]: This is basically a function to be performed on each element of the array. It accepts the first three items (currentValue, index, and array) as arguments.

Here are a few more examples:

  • We have an array of ages of members of a family going into a theatre to watch a movie rated PG 13+. We want to perform a check to ensure that no one under the age of 13 gets into the theatre:
const array = [12, 14, 29, 23, 25, 18];
console.log(array.some(num => num < 13)); // true

Unfortunately, there is a twelve-year-old kid, so we cannot allow the family into the theatre.

  • Alternatively we can also check if an array of kids going to play have an adult with them.
const array = [8, 10, 23, 9, 8, 12, 11];
console.log(array.some(num => num >= 18)); // true

This returns true because there is a number greater than 18, which represents the age of the adult in the midst of the kids.

  • Here is an example on strings that checks if an array of strings contain a vowel
const array = [ 'r', 's', 't', 'u', 'n', 'd'];
console.log(array.some(letter => 'aeiou'.includes(letter))); //true

Conclusion

Array.some() is great when you want to check if at least one item in an array meet a particular criteria. Alternative if you want to check if all the items in that array meet that criteria you should use array.every. I would be writing about this array method, follow me to get notified when it comes out @nedyudombat .

Got any other instances for the use of the Array.some() function? Please do well to share it in the comment section.

That's all for today, tomorrow we will talk about another set of functions used in array Iteration.

Here is the link to the other articles on this Array series written by me:

Got any question, addition or correction? Please leave a comment.

Thank you for reading. 👍

Top comments (3)

Collapse
 
nwamugo profile image
UGOJI Duziem

Thank you for sharing this insight on JavaScript array 'some' method.

After your first snippet of code, I noticed that you wrote that 'This loop runs until the test case passes.'
Actually, the loop runs until the test case passes or fails.

Also, do you think more comments on our code may speed up understanding? For example, a comment in the first snippet telling us that we are looping through to check for prime characteristics may help me the reader to quickly get what the code does.

Great effort!

Collapse
 
__naaza profile image
Naza

Educative

Collapse
 
nedyudombat profile image
Nedy Udombat

Thank you @__naaza