JavaScript Array Operations: Map

node v16.18.0
version: 1.0.0
endpointsharetweet
Map creates a new array populated with the results of calling a provided function on every element in the calling array.
/** * Map creates a new array populated with the results of calling a provided * function on every element in the calling array. * * @param {any} element - The current element. * @param {number} index (Optional) - The index of the current element. * @param {Array} array (Optional) - The array map was called upon. */ function transform (element, index, array) { /* return transformed element */ return "Number " + element.toString(); } [0, 1, 2, 3, 4, 5].map(transform);
// Let's start with an array const numbers = [0, 1, 2, 3, 4, 5];
// Let's operate over all the numbers with a map const doubles = numbers.map(number => number * 2);
// Let's perform a more complex operation const powers = doubles.map(number => Math.pow(number, 2));
Loading…

no comments

    sign in to comment