 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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 '0','undefined' and empty values from an array in JavaScript
To remove ‘0’. ‘undefined’ and empty values, you need to use the concept of splice(). Let’s say the following is our array −
var allValues = [10, false,100,150 ,'', undefined, 450,null]
Following is the complete code using for loop and splice() −
Example
var allValues = [10, false,100,150 ,'', undefined, 450,null] console.log("Actual Array="); console.log(allValues); for (var index = 0; index < allValues.length; index++) {    if (!allValues[index]) {       allValues.splice(index, 1);       index--;    } } console.log("After removing false,undefined,null or ''..etc="); console.log(allValues); To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo88.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo88.js Actual Array= [ 10, false, 100, 150, '', undefined, 450, null ] After removing false,undefined,null or ''..etc= [ 10, 100, 150, 450 ]
Advertisements
 