
- 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
Splitting last n digits of each value in the array in JavaScript
We have an array of literals like this −
const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799];
We are required to write a JavaScript function that takes in this array and a number n and if the corresponding element contains more than or equal to n characters, then the new element should contain only the last n characters otherwise the element should be left as it is.
Therefore, if n = 2, for this array, the output should be −
const output = [68, 65, 67, 3, 78, 78, 35, 99];
Example
Following is the code −
const arr = [56768, 5465, 5467, 3, 878, 878, 34435, 78799]; const splitLast = (arr, num) => { return arr.map(el => { if(String(el).length <= num){ return el; }; const part = String(el).substr(String(el).length - num, num); return +part || part; }); }; console.log(splitLast(arr, 2));
Output
This will produce the following output in console −
[ 68, 65, 67, 3, 78, 78, 35, 99 ]
- Related Articles
- How to split last n digits of each value in the array with JavaScript?
- Splitting number into n parts close to each other in JavaScript
- Splitting an array based on its first value - JavaScript
- Splitting an array into chunks in JavaScript
- Splitting an array into groups in JavaScript
- Splitting Number into k length array in JavaScript
- Splitting an object into an array of objects in JavaScript
- Program to find last two digits of 2^n in C++
- Returning an array containing last n even numbers from input array in JavaScript
- Find last k digits in product of an array numbers in C++
- JavaScript Splitting string by given array element
- Converting array to object by splitting the properties - JavaScript
- Splitting array of numbers into two arrays with same average in JavaScript
- Absolute difference between the first X and last X Digits of N?
- Find last two digits of sum of N factorials using C++.

Advertisements