
- 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
Remove leading zeros in a JavaScript array?
To remove leading zeroes in an array, use the concept of filter(). Following is our input wherein we will remove the leading zeroes −
[10,0,12,0,0]) [0,0,0,0,0,0,10,12,0]) [12,0,0,1,0,0])
Example
const removeLeadingZero = input => input.filter((lastValue => value => lastValue= lastValue || value) (false) ); console.log(removeLeadingZero([10,0,12,0,0])); console.log(removeLeadingZero([0,0,0,0,0,0,10,12,0])); console.log(removeLeadingZero([12,0,0,1,0,0]));
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo76.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo76.js [ 10, 0, 12, 0, 0 ] [ 10, 12, 0 ] [ 12, 0, 0, 1, 0, 0 ]
- Related Articles
- Remove leading zeros in array - JavaScript
- Remove leading zeros in array in JavaScript
- How to remove leading zeros from a number in JavaScript?
- Remove Leading Zeros from an Array using C++
- Remove Leading Zeros from a String in C#
- Remove Leading Zeros From String in Java
- Java Program to Remove leading zeros
- Golang program to remove leading zeros
- Remove leading zeros from a Number given as a string using Python
- How to pad a number with leading zeros in JavaScript?
- Python program to remove leading zeros from an IP address
- Add leading zeros to a MySQL column?
- JavaScript Regex to remove leading zeroes from numbers?
- Add leading Zeros to Python string
- Java Program to add leading zeros to a number

Advertisements