find() / findIndex()
method
ES6
find() returns the first element satisfying the test; findIndex() returns its index.
Syntax
array.find(callback) array.findIndex(callback)
Example
javascript
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' },
];
const bob = users.find(u => u.id === 2);
console.log(bob); // { id: 2, name: 'Bob' }
const idx = users.findIndex(u => u.name === 'Charlie');
console.log(idx); // 2