Answer the question
In order to leave comments, you need to log in
How to check if an associative array contains a certain JavaScript value?
Hello!
How to check if array {1: "", 2: "123", 3:"",...} contains empty values?
Thanks to all!
Answer the question
In order to leave comments, you need to log in
Iterate through each of the properties and check. If it evaluates to false when converted to boolean, then it is empty.
There are two exceptions: a true boolean false
and a number 0
, which will not be considered null.
var data = {1: "", 2: "123", 3:""};
function anyEmpty(obj) {
var p, v;
for( p in obj) {
v = obj[p];
if( !obj.hasOwnProperty(p)) continue;
if( v === false || v === 0) continue;
if( !v ) return true;
}
return false;
}
anyEmpty(data) // true
in js {} is an ordinary object, in this language objects play two roles, as data carriers and how an ordinary object
will return true if there is at least one empty value, null, undefined, "", false and so on.
will return the number of empty values
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question