Input: 235283 Output: [2, 3, 5, 2, 8, 3] Input: 8998123 Output: [8, 9, 9, 8, 1, 2, 3] Input: 1234567 Output: [1, 2, 3, 4, 5, 6, 7]
In this article, we will use two different methods to convert the given number into an array.
Array.from(object, mapFunction, thisValue)
- Store a number in a variable.
- Use Array.from() method and in its first parameter enter the string type cast value.
- In the second parameter, we use a function i.e. myFunc, at every iteration the function will be called.
- The myFunc function will take a parameter that was returned by the iteration of Array.from() method. We typecast the number into a string so the type of parameter is a string, but we will typecast it into integer and return it.
- The value returned by the Array.from() is our result.
[2, 3, 5, 3, 4, 5 ]
array.map(function(currentValue, index, arr), thisValue)
- 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.
[2, 3, 5, 3, 4, 5]