- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Array.prototype.map() function
The Array.prototype.map() function of JavaScript is used create a new array with the results of called function.
The syntax is as follows −
arr.map(function callback(currentValue[, index[, array]])
Let us now implement the Array.prototype.map() method in JavaScript −
Example
<!DOCTYPE html> <html> <body> <h2>Demo Heading</h2> <p>Click to display the abs() result...</p> <button onclick="display()">Result</button> <p id="test"></p> <script> function display() { var arr = [1, -2, 3, 4, 5, -6, 7, 8, 9, 10]; var res = arr.map(Math.abs); document.write(res); } </script> </body> </html>
Output
Click the “Result” button above −
Example
<!DOCTYPE html> <html> <body> <h2>Demo Heading</h2> <p>Click to round the numbers...</p> <button onclick="display()">Result</button> <p id="test"></p> <script> var arr = [11.4, 12.9, 25.6, 88.9]; document.getElementById("test").innerHTML = arr function display() { var res = arr.map(Math.round); document.getElementById("test").innerHTML = res } </script> </body> </html>
Output
Click the “Result” button to round the numbers −
Advertisements