Map
π¨βπΌ The
map method creates a new array by transforming each element.const numbers = [1, 2, 3]
const doubled = numbers.map((n) => n * 2)
// [2, 4, 6]
The callback receives each element and returns the transformed value.
- Use
mapto extract product names from an array of products - Use
mapto create formatted price strings - Use
mapto transform products into a different shape
π° Map syntax:
const result = array.map((element) => transformedValue)
π° TypeScript infers the result type from what you return:
const names = products.map((p) => p.name) // Array<string>