C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Array map() methodThe JavaScript array map() method calls the specified function for every array element and returns the new array. This method doesn't change the original array. SyntaxThe map() method is represented by the following syntax: array.map(callback(currentvalue,index,arr),thisArg) Parametercallback - It represents the function that produces the new array. currentvalue - The current element of array. index - It is optional. The index of current element. arr - It is optional. The array on which map() method operated. thisArg - It is optional. The value to use as this while executing callback. ReturnA new array whose each element generate from the result of a callback function. JavaScript Array map() method exampleLet's see some examples of map() method. Example 1Here, map() method returns the round of given elements to the nearest integer. <script> var arr=[2.1,3.5,4.7]; var result=arr.map(Math.round); document.writeln(result); </script> Output: 2,4,5 Example 2Here, map() method returns every element of given array by multiplying it by 3. <script> var arr=[2,4,6]; var result=arr.map(x=>x*3); document.writeln(result); </script> Output: 6,12,18
Next TopicJavaScript Array
|