How to Remove Empty Strings from an Array In JavaScript
When working with arrays in JavaScript, it's common to encounter scenarios where you need to remove empty strings from an array. Empty strings can clutter the array and affect the desired functionality of your code. Fortunately, JavaScript offers several methods to efficiently remove empty strings from an array. Yyou can effectively filter out the empty strings and obtain a new array containing only the non-empty string values. These methods provide flexibility and allow you to customize the filtering process based on your specific requirements.
In this article, we will explore different approaches to removing empty strings from an array in JavaScript, equipping you with the knowledge to streamline your array manipulation tasks and optimize your code.
Table Of Contents
01. Using "Array.filter()":
var array = ["Apple", "", "Banana", "", "Cherry", ""]; var filteredArray = array.filter(function (value) { return value !== ""; }); console.log(filteredArray);
Output:
["Apple", "Banana", "Cherry"]
In this method, Array.filter()
is used to create a new array containing only the non-empty strings. The callback function checks each element and returns true
if it is not an empty string.
Read Also:
02. Using "Array.reduce()"
var array = ["Apple", "", "Banana", "", "Cherry", ""]; var filteredArray = array.reduce(function (result, value) { if (value !== "") { result.push(value); } return result; }, []); console.log(filteredArray);
Output:
["Apple", "Banana", "Cherry"]
Here, Array.reduce()
is employed to iterate over the array and build a new array excluding the empty strings. The callback function checks each element and adds it to the result
array only if it is not an empty string.
Read Also:
- How To Generate Random Rgb Color Using Javascript
- How To Generate a Random Color in JavaScript
- How To Sort Alphabetically Html Unordered Elements Using JavaScript
- How to Append Text to a DIV using JavaScript
- How to Call a JavaScript Function on Page Load
- How to Get Random Value from Array in Javascript
- How to Get an Object Keys and Values in JavaScript
03. Using "for loop"
var array = ["Apple", "", "Banana", "", "Cherry", ""]; var filteredArray = []; for (var i = 0; i < array.length; i++) { if (array[i] !== "") { filteredArray.push(array[i]); } } console.log(filteredArray);
Output:
["Apple", "Banana", "Cherry"]
In this method, a for
loop is used to iterate over the array. Empty strings are skipped, and non-empty strings are added to the filteredArray
using the push()
method.
04. Using "Array.from()" and "Array.filter()"
var array = ["Apple", "", "Banana", "", "Cherry", ""]; var filteredArray = Array.from(array).filter(function (value) { return value !== ""; }); console.log(filteredArray);
Output:
["Apple", "Banana", "Cherry"]
Here, Array.from()
is utilized to convert the array-like object into a new array. Then, Array.filter()
is used to create a new array excluding the empty strings.
Read Also:
- How to print hello world using javascript
- How to redirect to another page using javascript
- How to refresh page on specific time using javascript
- How to remove a property of JavaScript object
- How to remove a specific item from an array in javascript
- How to scroll to the top of the page using javascript
- How to set default argument values in JavaScript functions
- How to validate an email address Using JavaScript
- What is the reverse of the push function in javascript
- Write a JavaScript function to check if an input is an array
Comments
Post a Comment