Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 267
Julian Berger Avatar asked Jul 21 '26 03:07

Julian Berger


1 Answers

const arrayElements = [
  { id: 1, value: 5 },
  { id: 2, value: 6 },
  { id: 3, value: 7 },
  { id: 4, value: 8 }
]

console.log(arrayElements.findIndex((item) => item.id === 3))
like image 109
Juliano Costa Avatar answered Jul 23 '26 16:07

Juliano Costa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!