Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the array has empty element or not

Tags:

javascript

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.

like image 224
ton1 Avatar asked Apr 14 '16 11:04

ton1


People also ask

How do you check if an array is empty or null?

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.

How check array is empty or not in JavaScript?

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.

How do you check if an array of strings is empty?

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 ).

How check if array is empty C?

✓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.


2 Answers

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
like image 135
Chiru Avatar answered Sep 30 '22 04:09

Chiru


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
like image 26
Oriol Avatar answered Sep 30 '22 04:09

Oriol