TypeScript Array findIndex()

In this chapter, we will explore the findIndex() method for arrays in TypeScript. This method is a built-in function that helps in finding the index of the first element in an array that satisfies a provided testing function. Understanding how to use findIndex() is useful for locating the position of elements within arrays based on specific criteria.

Table of Contents

  1. Definition
  2. Syntax
  3. Examples
  4. Conclusion

1. Definition

The findIndex() method returns the index of the first element in the provided array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

2. Syntax

array.findIndex(callback(element, index, array), thisArg?);

Parameters

  • callback: A function to execute on each value in the array, taking three arguments:
    • element: The current element being processed in the array.
    • index (optional): The index of the current element being processed in the array.
    • array (optional): The array findIndex was called upon.
  • thisArg (optional): Object to use as this when executing callback.

Return Value

The method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.

3. Examples

Let's look at some examples to understand how findIndex() works in TypeScript.

Example 1: Basic Usage

In this example, we find the index of the first element in the array that is greater than 10.

let numbers: number[] = [5, 12, 8, 130, 44];
let result = numbers.findIndex((num) => num > 10);
console.log(result); // Output: 1

Example 2: Finding the Index of an Even Number

In this example, we find the index of the first even number in the array.

let numbers: number[] = [1, 3, 7, 8, 5];
let result = numbers.findIndex((num) => num % 2 === 0);
console.log(result); // Output: 3

Example 3: Using Index and Array in Callback

In this example, we use the index and the array itself within the callback function to find an element.

let numbers: number[] = [1, 3, 7, 8, 5];
let result = numbers.findIndex((num, index, arr) => {
  console.log(`Element: ${num}, Index: ${index}, Array: ${arr}`);
  return num > 6;
});
console.log(result); // Output: 2
// Console will also log each element, index, and array.

Example 4: Finding the Index of an Object in an Array

In this example, we find the index of the first object in an array with a specific property value.

interface Person {
  name: string;
  age: number;
}

let people: Person[] = [
  { name: "Ravi", age: 25 },
  { name: "Ankit", age: 30 },
  { name: "Priya", age: 28 },
];

let result = people.findIndex((person) => person.age > 25);
console.log(result); // Output: 1

Example 5: No Match Found

In this example, we use findIndex() with a condition that no elements satisfy, resulting in -1.

let numbers: number[] = [1, 3, 5, 7];
let result = numbers.findIndex((num) => num > 10);
console.log(result); // Output: -1

4. Conclusion

In this chapter, we covered findIndex() method definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use findIndex() effectively can help in various array manipulation tasks in TypeScript, especially when locating the position of elements within arrays based on specific criteria.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare