Skip to content
Advertisement

indexOf() when array-elements are objects (javascript)

For instance, a variable named arrayElements of type array contains:
[{id:1, value:5},{id:2, value:6},{id:3, value:7},{id:4, value:8}].

How do I get the position of the array element with id === 3(3rd element) in the arrayElements variable besides using loop?

thanks.

Advertisement

Answer

You have to loop at one point. But you can abstract it to look like you’re not looping

JavaScript

As mentioned by Anthony, this is proposed for ECMAScript 6. Here’s the more complete polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement