How to use reduce method In Javascript

The reduce method iterates through array elements, comparing each with the target value. It returns the index when the value is found, updating the accumulator; otherwise, it returns -1.

Example: In this example, the reduce method iterates over each item in the array. If the current item matches the value we’re searching for and the accumulator is still -1 (meaning the item hasn’t been found yet), it returns the current index.

JavaScript
const fruits = ['apple', 'banana', 'cherry', 'orange'];
const value = 'cherry';

const index = fruits.reduce((acc, item, currentIndex) => {
    if (item === value && acc === -1) {
        return currentIndex;
    }
    return acc;
}, -1);

console.log(index); 

Output
2


Find the Array Index with a Value in JavaScript

Finding the index of a specific value in an array in JavaScript involves searching through the array’s elements to locate the position of the desired value. This index serves as a reference point for accessing or manipulating the value within the array.

Example 1:

Input: ['apple', 'banana', 'cherry', 'orange']
N = 'cherry'
Output: 2
Explanation: The index of the word cherry is 2

These are the following approaches by using these we can find the Array Index with a Value:

Table of Content

  • Using indexOf() method
  • Using findIndex() method
  • Using for loop
  • Using Lodash _.findIndex() Method
  • Using reduce method

Similar Reads

Using indexOf() method

We have an array of fruits and want to find the index of the element ‘cherry’.We use the indexOf() method to search for ‘cherry’ in the fruits array and it returns the index of ‘cherry’, which is 2....

Using findIndex() method

The findIndex() method in JavaScript is used to get the index of the initial element in an array that meets a particular condition or testing function.The method returns the index of the first element that passes the test or -1 if no element passes the test. The findIndex() method does not modify the original array....

Using for loop

Another approach to finding the index of an array with a specific value is to use a for loop to iterate through the array and compare each element with the value.The loop iterates through each element of the numbers array and checks if the current element is equal to 30.When element 30 is found at index 2, the loop is terminated using the break statement, and index 2 is stored in the index variable.Finally, the index variable is logged to the console, which outputs 2....

Using Lodash _.findIndex() Method

In this approach, we are using the lodash that is the library of JavaSCript for fucntions.It has inbuilt function _.findIndex() which can be used to find the index of the given value.We have to pass the given value of an array and it will return the index number....

Using reduce method

The reduce method iterates through array elements, comparing each with the target value. It returns the index when the value is found, updating the accumulator; otherwise, it returns -1....