Higher-Order Array Methods
map Transformations
Create a new array by transforming each value.
map Transformations
map_transform.js
const scale = 2;
const base = [2, 4, 6];
const adjusted = base.map((value) => value * scale);
console.log("scale=" + scale);
console.log("adjusted=" + adjusted.join(","));
Follow Each Element
- Start with
base:[2, 4, 6]. mapcalls the callback once for each value.- Each callback result goes into the new
adjustedarray. - The original
basearray stays unchanged. | Input value | Callback result | | --- | --- | |2|2 * scale| |4|4 * scale| |6|6 * scale|
map-transform
`map` returns a new array. The original array stays the same while the callback decides the replacement value for each element.
Exercise: map_transform.js
Use map to multiply each number in an array by a scale value and print the new array