JS Built-ins
← Array175 / 226
ArrayArray.prototype.someintermediate

Array.prototype.some() β€” find an admin user

πŸ“– Usage Example
const users = [{active:false}, {active:true}, {active:false}]
users.some(u => u.active)  // β†’ true

Use some() to test whether any object in an array has a particular property value.

Array.prototype.some() β€” objects

some() is commonly used to check whether an array of objects contains at least one item matching a condition.

Challenge: Implement hasAdmin(users) that returns true if any user has role === 'admin'.

hasAdmin([{role: 'user'}, {role: 'admin'}]) // β†’ true
hasAdmin([{role: 'user'}, {role: 'mod'}])   // β†’ false
πŸ’‘ Hints (2)
  • β€’ Destructure the object in the predicate for clarity: `({ role }) => role === 'admin'`.
  • β€’ Strict equality (`===`) is important β€” `'administrator'` is not `'admin'`.