map() array method in javascript
Table of contents
Array methods were introduced in javascript in ES5. Before this, every array iteration was performed by loops. But after the introduction of array methods to perform array operations, work on an array has become very easy without the implementation of extra coding.
Here we will discuss some array methods.
why use map()?
This method returns a new array of elements that contains the result of operations, which are applied to every array element.
It is like any other iteration method for, foreach, but it is more readable and more concise.
syntax
array.map(callbackfunction(element,index,array)=>statements);
parameters
callbackfunction
A function that executes elements of the array.
arguments
element: current element is being processed in the array.
index: the current index of the current element is being processed in the array.
array: This is the main array on which this method is being performed.
Return Value
It returns a new array of elements which results from functions.
Example
Let, you have a company which stores prices of items in string. If you want to price list in numbers not strings then
Output