Categories
JavaScript Answers

How to find last index of element inside array by certain condition with JavaScript?

Spread the love

To find last index of element inside array by certain condition with JavaScript, we use the map and lastIndexOf methods.

For instance, we write

const lastIndex = elements.map((e) => e.a).lastIndexOf("something");

to call map with a callback that returns property a‘s value in each object e in elements.

Then we call lastIndexOf to get the last index of the a value that equals 'something' in the returned array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *