How to map array values without using map method in JavaScript ?
Last Updated :
17 Jul, 2024
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 array element. This can also be implemented using a for loop in JavaScript.
Approach 1: 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:
JavaScript
const arr = [4, 5, 10, 3, 8, 6];
let result = [];
// Square function returns square of a number
const square = function (num) {
return num * num;
}
for (let i = 0; i < arr.length; i++) {
result.push(square(arr[i]));
}
// Expected output: [16 ,25, 100, 9, 64, 36]
console.log(result);
Output[ 16, 25, 100, 9, 64, 36 ]
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.
Using forEach() Method
This approach uses the forEach() method to iterate over each element in the array and apply the function to each element, then push the result into a new array.
Example:
JavaScript
const arr = [4, 5, 10, 3, 8, 6];
let result = [];
// Square function returns the square of a number
const square = function (num) {
return num * num;
};
arr.forEach(element => {
result.push(square(element));
});
// Expected output: [16, 25, 100, 9, 64, 36]
console.log(result);
Output[ 16, 25, 100, 9, 64, 36 ]
Using reduce()
In JavaScript, you can use reduce() to map array values by accumulating results in a new array. The callback function applies an operation to each element and pushes the result to the accumulator, which is initialized as an empty array.
Example:
JavaScript
let array = [1, 2, 3, 4, 5];
let newArray = array.reduce(function(accumulator, element) {
accumulator.push(element * 2); // Example operation: multiplying each element by 2
return accumulator;
}, []);
console.log(newArray); // Output: [2, 4, 6, 8, 10]
Using the for...of Loop
In this approach, we utilize the for...of loop to iterate over each element in the array, apply a function to transform each element, and then push the transformed values into a new array. This method provides a straightforward and readable way to map array values.
Syntax:
for (const element of array) {
// Transform element
newArray.push(transformedElement);
}
Example: In this example, we use the for...of loop to square each element in the input array and store the results in a new array.
JavaScript
const arr = [4, 5, 10, 3, 8, 6];
let result = [];
// Square function returns the square of a number
const square = function (num) {
return num * num;
};
// Iterate over each element using for...of loop
for (const element of arr) {
result.push(square(element));
}
// Expected output: [16, 25, 100, 9, 64, 36]
console.log(result);
Output[ 16, 25, 100, 9, 64, 36 ]
Similar Reads
How to serialize a Map in JavaScript ? In this article, we will discuss, the serialization of maps in JavaScript. Serialization is the conversion of an object or a data structure to another format that is easily transferrable on the network. In JavaScript, the format suitable for transferring is JSON string. So, we usually call the JSON.
2 min read
How to iterate over Map elements in JavaScript ? Map() is very useful in JavaScript it organises elements into key-value pairs. This method iterates over each element in an array and applies a callback function to it, allowing for modifications before returning the updated array." The Map() keyword is used to generate the object. The map() method
3 min read
How are elements ordered in a Map in JavaScript ? In JavaScript, a new object called Map was introduced in the ES6 version. A map is a collection of elements where each element is stored in a key, value pair. Map objects can store both objects as well as primitive data types. The elements of a map are iterable. Elements are always iterated in the i
2 min read
What is JavaScript Map and how to use it ? What is Map?A Map in JavaScript is a collection of key-value pairs where keys can be any data type. Unlike objects, keys in a Map maintain insertion order. It provides methods to set, get, delete, and iterate over elements efficiently, making it useful for data storage and retrieval tasks.Syntaxnew
2 min read
How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach
3 min read
How to map array values without using map method in JavaScript ? 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 array element. This can also be implemented using a for loop in JavaScript.Approach 1: For this, we can create two arrays, in which o
3 min read
What is the difference between Map and WeakMap in JavaScript ? In this article, we will talk about the difference between Map and WeakMap which are introduced by ES6. Javascript object supports only one key object. For supporting multiple key objects, Then Map comes on this path. Map: A Map is an unordered list of key-value pairs where the key and the value can
4 min read
How To Convert Map Keys to an Array in JavaScript? Here are the various methods to convert Map keys to an array in JavaScript1. Using array.from() MethodThe Array.from() method in JavaScript converts Map keys to an array by using 'Array.from(map.keys())'. JavaScriptlet map = new Map().set('GFG', 1).set('Geeks', 2); let a = Array.from(map.keys()); co
2 min read
How to convert a plain object into ES6 Map using JavaScript ? The task is to convert a JavaScript Object into a plain ES6 Map using JavaScript. we're going to discuss a few techniques. To understand the difference between a map and an object please go through the Map vs Object in JavaScript article. Below are the following approaches to converting a plain obje
2 min read
JavaScript - Use map() on an Array in Reverse Order Here are the different methods to use map() on an array in reverse order with JavaScript1. Using JavaScript array.reverse() methodThe idea is to use the .reverse() method just after applying the .slice() method. Then use the .map() method on the reversed array to perform the task.JavaScriptlet a = [
3 min read