添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
const filteredMarks = marks.filter(checkPass); // [90, 44, 76] - All matching elements const foundMark = marks.find(checkPass); // 90 - First Matched element const foundIndex = marks.findIndex(checkPass); // 0 - Index of First Matched element const indexOfZero = marks.indexOf(0); // 4 - Array index of the specified element const includesZero = marks.includes(0); // true - Arrays contains the specified element const somePass = marks.some(checkPass); // true - At least one element passes the condition marks.forEach(checkPassAndPrint); // Prints: 90 44 76 - Execute the function for each element

We can also find an object by property value using these methods. We only need to change the property value check code in the checkPass method.

const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 17 },
  { name: "Charlie", age: 30 },
  { name: "David", age: 15 }
// 1. Using filter function
function isAdult(person) {
    return person.age > 18;
// [{ name: "Alice", age: 25 }, { name: "Charlie", age: 30 }]
const adults = people.filter(isAdult);
// 2. Using an inline arrow function
const isAdult = (person: { age: number }) => person.age > 18;
// [{ name: "Alice", age: 25 }, { name: "Charlie", age: 30 }]
const adults = people.filter(person => person.age > 18);   

1. Methods to Find Elements in an Array

At a high level, we can use one of the following methods to find an element or element index in the array.

Method Description
filter() Returns an array of elements that satisfy the given filter expression/function.
find() Returns the first element that satisfies the specified boolean function, or undefined if no elements are found.
findLast() Returns the last element that satisfies the specified function, similar to find() .
findIndex() Returns the index of the first element that satisfies the specified boolean function, or -1 if no match is found.
findLastIndex() Returns the last index of the element that satisfies the specified function, similar to findIndex() .
indexOf() Returns the first index of a given element, or -1 if the value is not found.
lastIndexOf() Returns the last index of a given element.
includes() Returns true if the array contains the given value, otherwise returns false .
some() Returns true if at least one element satisfies the given boolean function, otherwise returns false .
forEach() Executes a given function for each element in the array.

2. Array.filter() – Get All Elements matching a Condition

The filter() method executes a given function against each element in the array and returns a new array containing elements that satisfy the given function .

In the following example, we are:

const marks = [90, 44, 76, 12, 0, 34] // function to check if student passed or not for a subject function checkPass(mark){ return mark > 35 // usage of filter method console.log( marks.filter(checkPass) ) //[ 90, 44, 76 ]

3. Array.find() – Find an Element matching a Condition

The find() method executes a function for each element in the array and returns the first element that satisfies the function, while findLast() method returns the last element that satisfies the function.

The find() and findLast() returns undefined if no elements are found.

In the following example, we are:

const marks = [90, 44, 76, 12, 0, 34] // function to check if student passed or not for a subject function checkPass(mark){ return mark > 35 // usage of find method console.log( marks.find(checkPass) ) //90 // usage of findLast method console.log( marks.findLast(checkPass) ) //76

4. Array.findIndex() – Find Index of Element matching a Condition

The findIndex() executes a given function against each element in the given array and returns the index of the first element that satisfies the given function, while findLastIndex() returns the index of the last element that satisfies the given function.

The findIndex() and findLastIndex() methods return -1 if no match is found.

In the following example, we are:

const marks = [90, 44, 76, 12, 0, 34] // function to check if student passed or not for a subject function checkPass(mark){ return mark > 35 // usage of findIndex method console.log( marks.findIndex(checkPass) ) //0 // usage of findLastIndex method console.log( marks.findLastIndex(checkPass) ) //2

Here 0 is the index of the element 90 and 2 is the index of the element 76 .

5. Array.indexOf() – Find Element Index by Element Value

The indexOf() starts the search from left to right and returns the index of the first element that matches the specified element , while lastIndexOf() returns the index of the last element that matches the given element.

The indexOf() and lastIndexOf() methods return -1 if no match is found.

In the following example, we are: