JS Built-ins
← Array83 / 226
ArrayArray.prototype.findLastIndexintermediate

Array.prototype.findLastIndex() β€” last active user index

πŸ“– Usage Example
const items = [{ok:true},{ok:false},{ok:true}]
items.findLastIndex(i => i.ok)  // β†’ 2

Use findLastIndex() to find the position of the last object matching a property check.

Array.prototype.findLastIndex() β€” objects

findLastIndex() with object arrays lets you locate the index of the last object satisfying a property condition.

Challenge: Implement lastActiveIndex(users) that returns the index of the last user with active: true.

lastActiveIndex([{active:false},{active:true},{active:false},{active:true}]) // β†’ 3
lastActiveIndex([{active:false}])                                           // β†’ -1
πŸ’‘ Hints (2)
  • β€’ `findLastIndex` scans from the end, so it naturally finds the last qualifying index.
  • β€’ The returned index can be used with `splice` or `slice` to operate on that element.