Understanding Javascript Array Series IX - Array Loops & Iteration Part VI
Tags: javascript, beginners, webdev
Published: 2019-10-09 21:27:34

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

{% link https://dev.to/nedyudombat/understanding-javascript-array-series-viii-array-loops-iteration-part-v-5had %}

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:
```javascript
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](https://stackoverflow.com/questions/40200089/number-prime-test-in-javascript).

Let's take a look at the syntax below:
```javascript
   // 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:
```javascript
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.
```javascript
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
```javascript
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`](https://www.w3schools.com/jsref/jsref_every.asp). 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:
* [What is an Array?](https://dev.to/nedyudombat/understanding-javascript-array-series-i-what-is-an-array-l1i)
* [Alternate ways of Creating an Array.](https://dev.to/nedyudombat/understanding-javascript-array-series-ii-alternate-ways-of-creating-an-array-26c8)
* [Array Properties](https://dev.to/nedyudombat/understanding-javascript-array-series-iii-array-properties-43co)
* [Array Loops & Iteration Part I] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-i-334j)
* [Array Loops & Iteration Part II] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-ii-115o)
* [Array Loops & Iteration Part III] (https://dev.to/nedyudombat/understanding-javascript-array-series-iv-array-loops-iteration-part-iii-4f38)
* [Array Loops & Iteration Part IV] (https://dev.to/nedyudombat/understanding-javascript-array-series-vii-array-loops-iteration-part-iv-3nb8)
* [Array Loops & Iteration Part V] (https://dev.to/nedyudombat/understanding-javascript-array-series-viii-array-loops-iteration-part-v-5had)

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

Thank you for reading. :thumbsup: