

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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: How to map array values without using "map" method?
We can map the elements of an array by using the looping methods in JavaScript. The map() method creates a new array that will produce the outputs of a function for each element of the array. The map method can also be implemented using the for loop in JavaScript.
Approach
We will create two arrays out of which the first array contains the array elements that need to be mapped whereas the second array contains the output of the following array. We will be using the JavaScript Array push() function for returning the values of a function in the output array.
Syntax
array.push(element1, element2, element, ... , elementN
For finding the length of the array, we can use the below syntax −
array.length
Example
In the below example, we will be using the array.push() method to push the mapped values into the resultant array.
# index.html
<!DOCTYPE html> <html> <head> <title> Mapping Array Elements </title> </head> <body> <h2 style="color:green"> Welcome To Tutorials Point </h2> </body> <script> var arr = [1, 2, 3, 4, 5, 6]; var result = []; let i; //This function will return the square of the number const square = function(num){ return num*num; } for(i=0; i< arr.length; i++){ result.push(square(arr[i])); } console.log(result); //Expected output: [1, 4, 9, 16, 25, 36] </script> </html>
Output
- Related Questions & Answers
- How to use JavaScript map() method to access nested objects?
- Map values to colors in Matplotlib
- How to convert array into array of objects using map() and reduce() in JavaScript
- Convert 2D array to object using map or reduce in JavaScript
- Return the maximum number in each array using map JavaScript
- How to use my object like an array using map function in JavaScript?
- Map object in JavaScript.
- IntStream map() method in Java
- LongStream map() method in Java
- Remove duplicates and map an array in JavaScript
- Join Map values into a single string with JavaScript?
- Sum 2D array in Python using map() function
- Convert object to a Map - JavaScript
- Map numbers to characters in JavaScript
- Object to Map conversion in JavaScript
Advertisements