How to use map() method In Javascript
Javascript map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally, the map() method is used to iterate over an array and call function on every element of the array.
Example: In this example, we use the array map method of JavaScript.
// Create an array of string
let stringArray = ["10", "21", "3", "14", "53"];
// Create a numberArray and using
// map function iterate over it
// and push it by typecasting into
// int using Number
let numberArray = stringArray.map(Number);
// Print the array of numbers
console.log(numberArray);
Output
[ 10, 21, 3, 14, 53 ]
How to convert array of strings to array of numbers in JavaScript ?
In this article, we have given an array of strings and the task is to convert it into an array of numbers in JavaScript.
Input: ["1","2","3","4","5"]
Output: [1,2,3,4,5]
Input: ["10","21","3","14","53"]
Output: [10,21,3,14,53]
There are two methods to do this, which are given below:
Table of Content
- Array traversal and typecasting
- Using map() method of JavaScript
- Using forEach loop of JavaScript
- Using reduce method of JavaScript
- Using a for loop
Contact Us