JS Built-ins
← Array49 / 226
ArrayArray.prototype.entriesintermediate

Array.prototype.entries() β€” find entry by value

πŸ“– Usage Example
const arr = [10, 20, 30]
for (const [i, v] of arr.entries()) {
  if (v === 20) console.log('found at', i)  // found at 1
}

Use entries() to find both the index and value when searching through an array.

Array.prototype.entries() β€” searching with index

By iterating over entries(), you can find the first [index, value] pair where the value matches a condition β€” useful when you need both the index and the value.

Challenge: Implement findIndexOf(arr, val) that uses entries() to return the first [index, value] pair where the value equals val, or null if not found.

findIndexOf(['a', 'b', 'c'], 'b') // β†’ [1, 'b']
findIndexOf(['a', 'b', 'c'], 'z') // β†’ null
πŸ’‘ Hints (2)
  • β€’ Use `for...of` with destructuring: `for (const [i, v] of arr.entries())`.
  • β€’ Return early as soon as the value matches to get the *first* occurrence.