
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How can I split an array of Numbers to individual digits in JavaScript?
We have an array of Number literals, and we are required to write a function, say splitDigit() that takes in this array and returns an array of Numbers where the numbers greater than 10 are splitted into single digits.
For example −
//if the input is: const arr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ] //then the output should be: const output = [ 9, 4, 9, 5, 9, 6, 9, 7, 9, 8, 9, 9, 1, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0, 6 ];
So, let’s write the code for this function, we will use the Array.prototype.reduce() method to split the numbers.
Example
const arr = [ 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106 ] const splitNum = (n, res = []) => { if(n){ return splitNum(Math.floor(n/10), [n % 10].concat(res)); }; return res; }; const splitDigit = (arr) => { return arr.reduce((acc, val) => acc.concat(splitNum(val)), []); }; console.log(splitDigit(arr));
Output
The output in the console will be −
[ 9, 4, 9, 5, 9, 6, 9, 7, 9, 8, 9, 9, 1, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0, 6 ]
- Related Articles
- How to split JavaScript Number into individual digits?
- Number Split into individual digits in JavaScript
- How to break or split number into individual digits in Excel?
- Split an array of numbers and push positive numbers to JavaScript array and negative numbers to another?
- How to split last n digits of each value in the array with JavaScript?
- How can I convert the arguments object to an array in JavaScript?
- How do I search through an array using a string, which is split into an array with JavaScript?
- Can split array into consecutive subsequences in JavaScript
- How can I convert an array to an object by splitting strings? JavaScript
- How to get the numbers which can divide all values in an array - JavaScript
- How can I remove a specific item from an array in JavaScript
- Print prime numbers with prime sum of digits in an array
- How can I remove a specific item from an array JavaScript?
- Find last k digits in product of an array numbers in C++
- Sum of individual even and odd digits in a string number using JavaScript

Advertisements