DEV Community

Nedy Udombat
Nedy Udombat

Posted on

Understanding Javascript Array Series XI - Array Loops & Iteration Part VIII

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

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

Array.find()

This method finds the first element of an array that satisfies a specified condition. Let's say you want to find the first even number in this array [1, 3, 5, 6, 7, 8, 9], the answer would be 6.

const arr = [1, 3, 5, 6, 7, 8, 9];
const even = arr.find(num => num%2 === 0);
console.log(even); //6

The loop runs until it gets to 6 and it returns 6. In a scenario where no element in the array meets the criteria, it returns undefined.

const arr = [1, 3, 5, 7, 9];
const even = arr.find(num => num%2 === 0);
console.log(even); //undefined

Let's take a look at the syntax

// syntax
arr.find(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 some examples:

  • Find the first person in this array that is old enough to buy a car(18).
  // array
  const playerArr = [
    { name: "Soji", age: 8},
    { name: "Chukwudi", age: 15},
    { name: "Nedy", age: 22},
    { name: "Ezekiel", age: 17},
    { name: "LII", age: 50},
  ]

  const firstEligibleCandidate = playerArr.find(player => player.age > 18);
  console.log(firstEligibleCandidate) //{ name: "Nedy", age: 22}

Conclusion

Array.find() is great when you want to find the first element of an array that meets a particular criterion. In a situation where you want to find the position(index) of where that element is we can use the Array.findIndex() method. 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.find() 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 (6)

Collapse
 
john_pels profile image
Ajeigbe John Oluwaseyi

Awesome! Educative!

Collapse
 
nedyudombat profile image
Nedy Udombat

Thank you so much Ajeigbe

Collapse
 
__naaza profile image
Naza

Next time use my name as example 😉😁

Collapse
 
nedyudombat profile image
Nedy Udombat

Sure will @__naza

Collapse
 
mremeka profile image
Ukpai Chukwuemeka

Awesome post.

Collapse
 
nedyudombat profile image
Nedy Udombat

Thank you Ukpai, I am glad you liked it