Home > Web Front-end > JS Tutorial > body text

How to convert an array of strings to an array of numbers in JavaScript?

WBOY
Release: 2023-09-12 20:21:04
forward
2201 people have browsed it

如何在 JavaScript 中将字符串数组转换为数字数组?

In JavaScript, there are several ways to convert an array of strings to an array of numbers. The first way is to use the built-in parseInt() method, the second way is to use the type conversion function, and the third way is to use the Unary operator.

Using the parseInt() method

The parseInt() method is a built-in function in JavaScript that accepts a string as a parameter and returns a number. This method can be used to convert an array of strings to an array of numbers.

Syntax

parseInt()The syntax of the method is as follows-

parseInt(string, radix);
Copy after login

Parameters

  • string - The string to be converted to a number.

  • Radix - An optional parameter that specifies the radix (radix) of the string. The default value is 10.

Example

The following example demonstrates how to use the parseInt() method to convert an array of strings to an array of numbers.

<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var arr = ["1", "2", "3"];
      // array of strings

      var nums = arr.map(function(str) {
         // using map() to convert array of strings to numbers

         return parseInt(str); });
      document.getElementById("result").innerHTML = nums
   </script>
</body>
</html>
Copy after login

In the above example, the map() method is used in the parseInt() method to convert each element in the arr array into a number. The map() method creates a new array containing the results of calling the function for each element in the array.

Using type conversion functions

Another way to convert a string array to a numeric array is to use a type conversion function. Type conversionThe function is a user-defined function used to convert one data type to another data type.

Example

In the following example, the type conversion function is used to convert an array of strings to an array of numbers.

<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      function toNumber(value) {
         return Number(value);
      }
      var arr = ["1", "2", "3"];
      var nums = arr.map(toNumber);
      document.getElementById("result").innerHTML = nums;
   </script>
</body>
</html>
Copy after login

In the above example, the toNumber() function converts a string to a number. This function is used to convert an array of strings to an array of numbers.

Using the unary operator

Another way to convert a string array to a numeric array is to use the unary operator. The unary operator is a prefix operator that converts its operand to a number.

Example

The following program shows how to use the unary operator to convert a string array to a numeric array.

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result"></div>
   <script>
      var arr = ["1", "2", "3"];
 
      // Using the unary + operator
      var nums = arr.map(unaryOp);
      document.getElementById("result").innerHTML = nums
     
      // Function that converts string to number
      function unaryOp(value) {
         return +value;
      }    
   </script>
</body>
</html>
Copy after login

In the above example, the unaryOp() function is a user-defined function that uses the unary operator to convert a string to a number. This function can be used to convert an array of strings to an array of numbers.

Conclusion

In JavaScript, there are two ways to convert an array of strings to an array of numbers. The first way is to use the built-in parseInt() method and the second way is to use a type conversion function.

The above is the detailed content of How to convert an array of strings to an array of numbers in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template