JS Built-ins
← Array55 / 226
ArrayArray.prototype.everyintermediate

Array.prototype.every() β€” check object properties

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

Use every() to confirm that a property holds true for all objects in an array.

Array.prototype.every() β€” object arrays

every() works on any array, including arrays of objects. You can check that all objects satisfy a structural requirement.

Challenge: Implement allHaveName(objs) that returns true if every object in the array has a .name property that is a non-empty string.

allHaveName([{name:'Alice'},{name:'Bob'}]) // β†’ true
allHaveName([{name:'Alice'},{age:30}])     // β†’ false
πŸ’‘ Hints (2)
  • β€’ Use `'name' in obj` to check property existence, or just check `typeof obj.name === 'string'`.
  • β€’ Cast to `Record<string, unknown>` to access dynamic properties without TypeScript errors.