Iain Freestone

❓ JavaScript Q & A No. 1 - How do I change every value in an array?

May 30, 2020

Use case

Take an array of numbers and double every value in the array.

Answser

const myArray = [1, 2, 3, 4, 5];

// Function that returns the input * 2
const double = (x) => x * 2;

// Plain JavaScript
myArray.map(double);

//Ramda
R.map((item) => item * 2, myArray);

//Lodash
_.map(myArray, double);

// Result
// [ 2, 4, 6, 8, 10 ]

Reference

Methods used

JavaScript - Array.prototype.map

Ramda - map

Lodash - map