Javascript array find method

The find() method in JavaScript is used to retrieve the first element in an array that satisfies a given condition. It executes a provided function once for each element in the array until it finds a value for which the function returns true. If such an element is found, find() returns the element; otherwise, it returns undefined.

Note that if there are multiple elements that satisfy the condition, find() returns the first occurrence encountered during the iteration. If no matching element is found, undefined is returned.

Here's the basic syntax of the find() method:

array.find(callback(element[, index[, array]])[, thisArg])

Example 1: Finding an element greater than 5 in an array of numbers


 const numbers = [2, 4, 6, 8, 10];
 const foundNumber = numbers.find(element => element > 5);
 console.log(foundNumber); // Output: 6

Example 2: Finding an object by a specific property value in an array of objects

 const users = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Jane' },
    { id: 3, name: 'Alice' }
  ];
 
  const foundUser = users.find(user => user.id === 2);
console.log(foundUser); // Output: { id: 2, name: 'Jane' }
 

In this example, we have an array of user objects. We use the find() method to search for the first user object with an id property equal to 2. The callback function (user) => user.id === 2 checks if the id property of each user object matches 2. Once a match is found, the find() method returns the corresponding user object.

Example 3: Finding a string that starts with a specific letter in an array of strings

 
 const fruits = ['apple', 'banana', 'orange', 'grape'];
 const foundFruit = fruits.find(fruit => fruit.startsWith('b'));
 console.log(foundFruit); // Output: 'banana'

Example 4: Finding an element based on an index


 const numbers = [10, 20, 30, 40, 50];
 const foundElement = numbers.find((element, index) => index === 2);
 console.log(foundElement); // Output: 30

In this example, we have an array of numbers. We use the find() method to search for the element at index 2. The callback function (element, index) => index === 2 checks if the index of each element is equal to 2. The find() method returns the element at the specified index.

Example 4: Using a custom this value

 
   function checkElement(element) {
    return element === this;
   }
 
   const numbers = [10, 20, 30, 40, 50];
   const foundElement = numbers.find(checkElement, 30);
   console.log(foundElement); // Output: 30
 

In this example, we define a custom function checkElement that compares each element to the provided this value. We pass checkElement as the callback function to the find() method and also specify 30 as the thisArg parameter. The find() method executes checkElement with 30 as the this value, and the function checks if each element is equal to 30. Once a match is found, the find() method returns the matching element.

Example 5: Finding an element in a multidimensional array


 const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
   ];
 
   const foundElement = matrix.find(row => row.includes(5));
   console.log(foundElement); // Output: [4, 5, 6]
 

Example 6: Finding an object with a specific property value

 
const students = [  { name: 'John', age: 20 },  { name: 'Jane', age: 22 },  { name: 'Alice', age: 19 }];
const foundStudent = students.find(student => student.name === 'Jane');console.log(foundStudent); // Output: { name: 'Jane', age: 22 }