Skip to content
Related Articles

Related Articles

How to map array values without using map method in JavaScript ?

Improve Article
Save Article
Like Article
  • Last Updated : 06 Dec, 2021

Array elements can be mapped by using looping methods in JavaScript. The map() method creates a new array with the results of the output of a function called for each element of the array. This can also be implemented using for loop in JavaScript.

Approach: For this, we can create two arrays, in which one array contains the array elements that are to be mapped, and the second array stores all the return values of the corresponding function.  We can use the JavaScript Array push() method to push the return values of the function in the output array.

Syntax:

array.push(element1, element2, element, ... , elementN )

The Array length method can be used to find the length of the corresponding array.

Syntax:

array.length

Return value: Number

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <title>
        JavaScript mapping using loops
    </title>
</head>
  
<body>
    <script>
        var arr = [4, 5, 10, 3, 8, 6];
        var result = [];
        let i;
  
        //square function returns square of a 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: [16 ,25, 100, 9, 64, 36]
  
    </script>
</body>
  
</html>


Output: The indices of the elements in the output array are shown before the numbers in the output as well as the length of the output array.

[16, 25, 100, 9, 64, 36]
0: 16
1: 25
2: 100
3: 9
4: 64
5: 36
length: 6
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!