JS Built-ins
← Array68 / 226
ArrayArray.prototype.findintermediate

Array.prototype.find() β€” find object by id

πŸ“– Usage Example
const users = [{id:1, name:'Alice'}, {id:2, name:'Bob'}]
users.find(u => u.id === 2)  // β†’ {id:2, name:'Bob'}

Use find() to locate the first object in an array that matches a property condition.

Array.prototype.find() β€” objects

find() is commonly used to look up an object in an array by a unique property such as an id.

Challenge: Implement findById(items, id) that returns the first item whose id matches.

findById([{id:1,name:'A'},{id:2,name:'B'}], 2) // β†’ {id:2, name:'B'}
findById([{id:1,name:'A'}], 99)                // β†’ undefined
πŸ’‘ Hints (2)
  • β€’ Use strict equality `===` when comparing ids to avoid type coercion.
  • β€’ `find()` returns the actual object reference, not a copy.