Categories
JavaScript Answers

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

Spread the love

Sometimes, we want to find last index of element inside array by certain condition with JavaScript.

In this article, we’ll look at how to find last index of element inside array by certain condition with JavaScript.

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

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 and lastIndex to return an array with the a property value of each item in elements.

And then we call lastIndexOf to look for the last index of 'something' in the returned array.

Conclusion

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

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 *