JS Built-ins
← Array134 / 226
ArrayArray.prototype.mapintermediate

Array.prototype.map() β€” pluck a property from objects

πŸ“– Usage Example
const users = [{name:'Ana'}, {name:'Ben'}]
users.map(u => u.name)  // β†’ ['Ana', 'Ben']

Use map() to extract a single property from each object in an array.

Array.prototype.map() β€” property extraction

A common use of map is extracting a single property from each object in an array β€” sometimes called "plucking". TypeScript generics let you express this in a fully type-safe way.

Challenge: Implement pluck(arr, key) that returns an array of the value at key for every object in arr.

pluck([{ name: 'Alice' }, { name: 'Bob' }], 'name') // β†’ ['Alice', 'Bob']
pluck([{ age: 30 }, { age: 25 }], 'age')            // β†’ [30, 25]
πŸ’‘ Hints (2)
  • β€’ `K extends keyof T` ensures the key actually exists on type `T`.
  • β€’ The return type `T[K][]` is TypeScript's way of saying "array of the value type at key K".