Skip to content
Advertisement

Return index value from filter method javascript

I have an array of objects in my angular controller.

I want to return the value of the index of the field within the array which has a matching ID to my parameter.

There will only be one object in the array with a matching fieldId..

JavaScript

Advertisement

Answer

You can’t return index from filter method.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

You can use forEach

JavaScript

or even better to use for as you can’t stop forEach when you have found your id.

JavaScript
Advertisement