How to use findIndex() Method In Javascript
The Javascript Array.findIndex() method is used to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling). Otherwise, if no data is found then the value of -1 is returned.
Example: In this example, we have used findIndex() Method
Javascript
let arrayObj = [{ prop_1: 'val' , prop_2: 'val_12' , prop_3: 'val_13' }, { prop_1: 'val' , prop_2: 'val_22' , prop_3: 'val_23' }]; const index = arrayObj.findIndex(object => { return object.prop_3 === 'val_23' ; }); console.log(index); |
Output
1
JavaScript Get the index of an object by its property
Given an object, the task is to get the object’s index from the array of objects of the given property name and property value using JavaScript. we’re going to discuss a few techniques.
Below are the following approaches:
Table of Content
- Using Array map() Method
- Using for loop
- Using findIndex() Method
- Using some() Method
Contact Us