How can I check the array has empty element or not? Imagine this array,
var arr = [ 'a', 'b', , 'd'];
the arr[2] is undefined
. I want to check this. If the element has empty element, return 'true' or return false. Maybe like this,
function hasEmptyElement(array){
for (var i=0; i<array.length; i++){
if (typeof arr[i] == 'undefined'){
return true;
// and then ?
// should I use double for loop or helper variable?
}
}
}
I confuse how can I do this. Please help me the clevers.
To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null. The array is empty.
Having confirmed that the variable is an array, now we can check the length of the array using the Array. length property. If the length of the object is 0, then the array is considered to be empty and the function will return TRUE. Else the array is not empty and the function will return False.
To determine whether a string array has empty strings (string elements with zero characters), use the == operator. For example, if str is a string containing zero characters, then str == "" returns logical 1 ( true ).
✓to check whether an array is empty or not just iterate the elements of the array and compare them with null character '/0'. ✓you can also declare an empty array like this arr[]={}. Then use the sizeof function, if it returns 0 your array is empty.
As of ES2016, you should use Array.prototype.includes:
const array = ["a", "b", , "d"];
array.includes(undefined); // true
(You don't need to write undefined
, but this makes it more clear what's happening.)
Note that this method treats slots valued undefined
and empty slots the same, although they're not. If you need to differentiate these two cases as well, starting from ES2017, you can use Object.values making the following expression true
if there are empty slots in the array:
Object.values(array).length !== array.length; // true
First, note the difference between empty slots and slots with undefined value:
var arr = [/*empty slot*/, undefined];
Object.keys(arr); // ["1"] but not "0"
"0" in arr; // false
"1" in arr; // true
ES5 array methods skip empty slots. ES6 [].includes
does not.
That means you can use
arr.includes(undefined); // has empty slot OR contains undefined value
arr.indexOf(undefined) > -1; // contains undefined value
If you want to test only if there are empty slots, you can iterate manually with a for
loop and check whether all indices between 0 and the length of the array are present, e.g. with in
operator.
(function() {
for(var i=0; i<arr.length; ++i) if(!(i in arr)) return true;
return false;
})(); // has empty slot
Or you can also use ES5 array methods and check if they skipped an index.
var n = 0;
arr.some((_,i) => i !== n++); // has empty slot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With