How can I check JavaScript arrays for empty strings?

I need to check if the array contains at least one empty element. If any of one element is empty, it will return false.

Example:

var my_arr = new Array(); my_arr[0] = ""; my_arr[1] = " hi "; my_arr[2] = ""; 

Elements of the 0th and 2nd array are empty.

+8
javascript arrays
source share
10 answers

You need to go through a cycle.

 function checkArray(my_arr){ for(var i=0;i<my_arr.length;i++){ if(my_arr[i] === "") return false; } return true; } 
+9
source share

You can check by going through the array with a simple for , for example:

 function NoneEmpty(arr) { for(var i=0; i<arr.length; i++) { if(arr[i] === "") return false; } return true; } 

Here you can try here , the reason we do not use .indexOf() is the lack of support in IE, otherwise it's' Even simpler:

 function NoneEmpty(arr) { return arr.indexOf("") === -1; } 

But alas, IE does not support this function on arrays, at least not yet.

+17
source share

You can try the jQuery.inArray () function :

 return jQuery.inArray("", my_arr) 
+7
source share

For this you can make an easy way to help:

 function hasEmptyValues(ary) { var l = ary.length, i = 0; for (i = 0; i < l; i += 1) { if (!ary[i]) { return false; } } return true; } //check for empty var isEmpty = hasEmptyValues(myArray); 

EDIT. This checks for false , undefined , NaN , null , "" and 0 .

EDIT2: Wrong waiting.

.. Fredrick

+3
source share
 function containsEmpty(a) { return [].concat(a).sort().reverse().pop() === ""; } alert(containsEmpty(['1','','qwerty','100'])); // true alert(containsEmpty(['1','2','qwerty','100'])); // false 
+3
source share

I see in your comments the question that the sample code you are giving is PHP, so I was wondering if you are really going to use PHP? In PHP, it will be:

 function hasEmpty($array) { foreach($array as $bit) { if(empty($bit)) return true; } return false; } 

Otherwise, if you really need JavaScript, I refer to Nick Craver's answer

+1
source share

Just do len(my_arr[i]) == 0; inside the loop to check if the string is empty or not.

+1
source share

Using a "higher order function" such as filter instead of loops can sometimes make code faster, safer, and more readable. Here you can filter the array to remove elements that are not an empty string, and then check the length of the resulting array.

Basic javascript

 var my_arr = ["", "hi", ""] // only keep items that are the empty string new_arr = my_arr.filter(function(item) { return item === "" }) // if filtered array is not empty, there are empty strings console.log(new_arr); console.log(new_arr.length === 0); 

Modern Javascript: Single Line

 var my_arr = ["", "hi", ""] var result = my_arr.filter(item => item === "").length === 0 console.log(result); 

Performance Note

Looping in this case is most likely faster, since you can stop looping as soon as you find an empty string. I could still use filter for code brevity and readability, but any strategy is justified.

If you need to iterate over all the elements in an array, perhaps however-- to check if each element is an empty string-- filter will be much faster than a for loop!

+1
source share
 var containsEmpty = !my_arr.some(function(e){return (!e || 0 === e.length);}); 

This checks for 0, false, undefined, "" and NaN. It is also one liner and works for IE 9 and above.

0
source share

Single line solution to check if a line has an empty element

 let emptyStrings = strArray.filter(str => str.trim().length <= 0); 

 let strArray = ['str1', '', 'str2', ' ', 'str3', ' '] let emptyStrings = strArray.filter(str => str.trim().length <= 0); console.log(emptyStrings) 

One line solution for getting non-empty strings from an array

 let nonEmptyStrings = strArray.filter(str => str.trim().length > 0); 

 let strArray = ['str1', '', 'str2', ' ', 'str3', ' '] let nonEmptyStrings = strArray.filter(str => str.trim().length > 0); console.log(nonEmptyStrings) 
0
source share

All Articles