Here are two ways to get an index of an object in an array in JavaScript:
- Using Array.findIndex()
- Using Array.map()
Method 1: Using an Array.findIndex()
Array.prototype.findIndex() method returns the index of the first element in the array that satisfies the provided testing function. No element passed the test if the findIndex() method returns -1.
Here is a code example:
const marks = [30, 100, 17, 23, 52, 20];
let index = marks.findIndex(marks => marks > 90);
console.log(index);
Output
1
Method 2: Using the Array.map() method
You can use the Array.map() method to get the index indirectly by transforming the array into an array of indices and then using Array.indexOf().
Here is a code example:
const marks = [30, 100, 17, 23, 52, 20];
const indices = marks.map((mark, idx) => (mark > 90 ? idx : -1));
const index = indices.indexOf(Math.max(...indices));
console.log(index);
Output
1
Here, we map the “marks” array to an array of indices based on the condition and then use Array.indexOf() to find the first non-negative index.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.