Warning: session_start(): open(/tmp/sess_262f9389f5cfd6dbb832f61883f3e9d9, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Remove Empty Strings From An Array In JavaScript - LearnShareIT

How To Remove Empty Strings From An Array In JavaScript

Remove Empty Strings From An Array In JavaScript

Removing Empty Strings from an array in JavaScript is not difficult, but it can not be easy with beginners if you don’t have the instructions. We will accompany you to solve this problem. There are some solutions can remove empty Strings from an array in JavaScript. Let’s learn about it with the explanation and examples below.

Remove Empty Strings From An Array In JavaScript

What is a String in JavaScript?

Like many programming languages, String is the most important type in JavaScript. A String is a sequence of characters. Strings in JavaScript are surrounded by either single quotation marks, double quotation marks, grave accents, or String objects in JavaScript.

Look at the example below to know more about it.

// create a string named 'str1' with single quotation marks.
let str1 = 'Hello everyone. This guide will help convert a String to the title case in JavaScript.'

// create a string named 'str1' with single quotation marks.
let str2 =" I am crvt4722."

// create a string named 'str3' with object String.
let str3 = new String(" Date: 11/09/2022.")

// create a string named ‘str4’ with grave accents.
let str4 = `Title: Convert A String To Title Case In JavaScript.`

console.log(str1)
console.log(str2)
console.log(str3)
console.log(str4)

Output 

Hello everyone. This guide will help convert a String to the title case in JavaScript.
I am crvt4722.
String {' Date: 11/09/2022.'}
Title: Convert A String To Title Case In JavaScript.

What is an empty String in JavaScript?

An empty String is a String that length is 0. It doesn’t have any characters.

Look at the example below to know more about it.

// create an empty string named ‘str’
let str =''

Solution 1: Using filter() method in JavaScript

Syntax 

array.filter(function(currentValue, index, arr), thisValue)

Parameters

  • function(): A function runs for each element in the array.
  • currentValue: The value of the current element.
  • index: The index of the current element.
  • arr: The array of the current element.
  • thisValue: The value that passed the function.

Return Value

An array contains the elements which pass the function.

Look at the example below to know about this method.

// create an array named ‘arr’
const arr = ["Learn", "", "To", "", "Share", "",  "IT"];

const res =  arr.filter(e =>  e.length >0);
console.log(res);

Output

['Learn', 'To', 'Share', 'IT']

Solution 2: Using map() method to iterate the array

Syntax

array.map(function(currentValue, index, arr), thisValue)

Parameters

  • function(): A function runs for each element in the array.
  • currentValue: The value of the current element.
  • index: The index of the current element.
  • arr: The array of the current element.
  • thisValue: The value that passed the function.

Return Value

An array contains the elements which pass the function.

Look at the example below to know about this method.

//create an array named ‘arr’.
const arr = ["Learn", "", "To", "", "Share", "",  "IT"];

const res = [];

arr.map(value => {
  if (value !== '') {
    res.push(value);
  }
});

console.log(res);

Output

['Learn', 'To', 'Share', 'IT']

Summary

These are some solutions that can help you remove empty Strings from an array in Javascript. To solve this problem, you can use JavaScript Array Iteration, such as the filter() method and the map() method like above. Choose the solution that is the most suitable for you. We hope this tutorial is helpful to you. Have an exciting learning experience. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *