

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove leading zeros in array - JavaScript
The requirements for this question are simple. We are required to write a JavaScript function that takes in an array of Numbers. If the array contains leading zero, the function should remove the leading zeros in place, otherwise the function should do nothing.
For example −
If the input array is −
const arr = [0, 0, 0, 14, 0, 63, 0];
Then the output should be −
const output = [14, 0, 63, 0];
Therefore, let's write the code for this function
Example
Following is the code −
const arr = [0, 0, 0, 14, 0, 63, 0]; const removeLeadingZero = arr => { while (arr.indexOf(0) === 0) { arr.shift(); }; }; removeLeadingZero(arr); console.log(arr);
This will produce the following output on console −
[ 14, 0, 63, 0 ]
- Related Questions & Answers
- Remove leading zeros in array in JavaScript
- Remove leading zeros in a JavaScript array?
- Remove Leading Zeros from an Array using C++
- Java Program to Remove leading zeros
- Remove Leading Zeros From String in Java
- How to remove leading zeros from a number in JavaScript?
- Remove Leading Zeros from a String in C#
- Python program to remove leading zeros from an IP address
- How to pad a number with leading zeros in JavaScript?
- Add leading Zeros to Python string
- Print leading zeros with C++ output operator
- Add leading zeros to a MySQL column?
- Add leading Zeros to Python Program string
- JavaScript Regex to remove leading zeroes from numbers?
- Java Program to add leading zeros to a number
Advertisements