How to use map() Method In Javascript
- In this aproach, we will Store the integer value in a variable.
- Typecast the integer into a string.
- Using the split() method to make it an array of strings.
- Iterate over that array using the map() method.
- Using the map() method returns the array of strings into an array of Integers.
Syntax:
array.map(function(currentValue, index, arr), thisValue)
Example: This example converts a number into an array using Array.map() method in Javascript.
// Declare a variable and store an
// integer value
let num = 235345
// Here we typecasting the num
// Splitting the num, so that
// we got an array of strings
// Then use map function to
// convert the array of strings
// into array of numbers
let myArr = String(num).split("").map((num) => {
return Number(num)
})
console.log(myArr)
Output:
[2, 3, 5, 3, 4, 5]
How to convert a number into array in JavaScript ?
In this article, we have been given a number and the task is to convert the given number into an array using JavaScript.
These are the following methods by using these we can solve this problem:
Table of Content
- Using Array.from() Method
- Using map() Method
- Using reduce() function
- Using Lodash _.toArray() Method
- Using mathematical operations
- Using a for loop to iterate through the string representation