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

  1. Start with base: [2, 4, 6].
  2. map calls the callback once for each value.
  3. Each callback result goes into the new adjusted array.
  4. The original base array 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