When working with JavaScript arrays, you might want to create a new array with the results of calling a provided function on every element in the previous/original array.
JavaScript has a very nifty function called Array.prototype.map() that you can use to do this.
Array.prototype.map() accepts a callback function as a parameter.
The callback function produces an element of the new Array and takes three arguments:
- currentValue - This is the current element being processed in the array.
- index (optional) - This is the index of the current element being processed in the array.
- array (optional) - The original array that map was called upon.
- thisArg (optional) - Value that you can use as this when executing the callback
Example usage:
const names = ['mike', 'john', 'anne'];
const namesUppercase = names.map(name => name.toUpperCase());
console.log(namesUppercase);
namesUppercase = ["MIKE", "JOHN", "ANNE"]
Top comments (0)