Find First Array Element Matching A Condition In JavaScript

To find first array element matching a condition in JavaScript, you can use the find() method. This method executes the callback function once for each element in the array until it finds a element that returns a truthy value for the condition provided. The find() method returns the value of the first element in the array that passes the test implemented by the callback function.

find first array element matching a condition in javascript

find() To Find First Array Element Matching A Condition In JavaScript

The find() method is used to return the value of the first element in an array that passes a test implemented by a callback function.

For example, you can use the find() method to get the first element in an array that is greater than 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

const first = numbers.find(x => x > 10);

console.log(first); // undefined

If no element in the array passes the test, the find() method returns undefined.

You can also use the find() method to get the first element in an array that is less than 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

const first = numbers.find(x => x < 10);

console.log(first); // -5

If no element in the array passes the test, the find() method returns undefined.

You can also use the find() method to get the first element in an array that is equal to 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

const first = numbers.find(x => x === 10);

console.log(first); // undefined

If no element in the array passes the test, the find() method returns undefined.

findIndex() To Find First Array Element Matching A Condition In JavaScript

The findIndex() method is used to return the index of the first element in an array that passes a test implemented by a callback function.

For example, you can use the findIndex() method to get the index of the first element in an array that is greater than 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

const first = numbers.findIndex(x => x > 10);

console.log(first); // -1

If no element in the array passes the test, the findIndex() method returns -1.

You can also use the findIndex() method to get the index of the first element in an array that is less than 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

const first = numbers.findIndex(x => x < 10);

console.log(first); // 0

If no element in the array passes the test, the findIndex() method returns -1.

You can also use the findIndex() method to get the index of the first element in an array that is equal to 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

const first = numbers.findIndex(x => x === 10);

console.log(first); // -1

If no element in the array passes the test, the findIndex() method returns -1.

for Loop To Find First Array Element Matching A Condition In JavaScript

You can also use a for loop to find the first array element that matches a condition.

For example, you can use a for loop to get the first element in an array that is greater than 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

let first;

for (const number of numbers) {

  if (number > 10) {

    first = number;

    break;

  }

}

console.log(first); // undefined

If no element in the array passes the test, the first variable will be undefined.

You can also use a for loop to get the first element in an array that is less than 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

let first;

for (const number of numbers) {

  if (number < 10) {

    first = number;

    break;

  }

}

console.log(first); // -5

If no element in the array passes the test, the first variable will be undefined.

You can also use a for loop to get the first element in an array that is equal to 10.

const numbers = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5];

let first;

for (const number of numbers) {

if (number === 10) {

  first = number;

  break;

}

}

console.log(first); // undefined

If no element in the array passes the test, the first variable will be undefined.

Conclusion

In this article, you’ve learned how to use the find() and findIndex() methods to get the first element in an array that matches a condition. You’ve also learned how to use a for loop to find the first element in an array that matches a condition.

If you have any questions, please leave a comment below. I would be happy to hear from you.

Leave a Reply