Example 1 — Official Samples: Array and Object
Double every number in an array, then every value in an object.
const double = x => x * 2;
console.log(R.map(double, [1, 2, 3])); // [2, 4, 6]
console.log(R.map(double, { x: 1, y: 2, z: 3 })); // { x: 2, y: 4, z: 6 } How It Works
- One-item function.
doubleonly knows how to change a single number. - Array.
R.mapcallsdoubleon1,2,3and collects[2, 4, 6]in a new array. - Object. For a plain object, it keeps the keys
x,y,zand transforms each value. - Same shape. An array gives back an array; an object gives back an object.
