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
Method 1: Array traversal and typecasting
In this method, we traverse an array of strings and add it to a new array of numbers by typecasting it to an integer using the parseInt() function.
Example: In this example, we are using Array traversal and typecasting.
// Create an array of string let stringArray = [ "1" , "2" , "3" , "4" , "5" ];
// Create an empty array of number let numberArray = []; // Store length of array of string // in letiable length length = stringArray.length; // Iterate through array of string using // for loop // push all elements of array of string // in array of numbers by typecasting // them to integers using parseInt function for (let i = 0; i < length; i++)
// Instead of parseInt(), Number()
// can also be used
numberArray.push(parseInt(stringArray[i]));
// Print the array of numbers console.log(numberArray); |
[ 1, 2, 3, 4, 5 ]
Method 2: Using map() method of 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); |
[ 10, 21, 3, 14, 53 ]
Method 3: Using forEach loop of JavaScript
The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
Example: In this example, we are using the array forEach loop of JavaScript.
// Create an array of string let stringArray = [ "10" , "21" , "3" , "14" , "53" ];
// Create a numberArray and using // forEach loop iterate over it // and push it to numberArray with typecasting it into // int using + operator let numberArray = []; stringArray.forEach( ele => numberArray.push(+ele)) // Print the array of numbers console.log(numberArray); |
[ 10, 21, 3, 14, 53 ]
Method 4: Using reduce method of JavaScript
The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
Example: In this example, we are using the reduce function of JavaScript.
// Create an array of string let stringArray = [ "10" , "21" , "3" , "14" , "53" ];
// Create a numberArray and using // reduce function iterate over it // and push it to numberArray with typecasting it into // int using + operator let numberArray = stringArray.reduce( (acc, x ) => acc.concat(+x), []) // Print the array of numbers console.log(numberArray); |
[ 10, 21, 3, 14, 53 ]