How to check if a javascript object contains a null value or is it null itself

Let's say that I access a javascript object called jso in Java, and I use the following statement to check if it is null

if (jso == null)

However, this statement returns true when jso contains some NULL values, which I don't want.

Is there any method that can distinguish between a null JavaScript object and a JavaScript object that contains some null values?

thank

+5
source share
3 answers

To determine if the target link contains an element with a null value, you will have to write your own function, since you have nothing to do for this. One simple approach:

function hasNull(target) {
    for (var member in target) {
        if (target[member] == null)
            return true;
    }
    return false;
}

, , , target , false. :

var o = { a: 'a', b: false, c: null };
document.write('Contains null: ' + hasNull(o));

:

null: true

, false:

var o = { a: 'a', b: false, c: {} };
document.write('Contains null: ' + hasNull(o));
+10

. .

var jso;
document.writeln(typeof(jso)); // 'undefined'
document.writeln(jso); // value of jso = 'undefined'

jso = null;
document.writeln(typeof(jso)); // null is an 'object'
document.writeln(jso); // value of jso = 'null'

document.writeln(jso == null); // true
document.writeln(jso === null); // true
document.writeln(jso == "null"); // false

http://jsfiddle.net/3JZfT/3/

+4
+2
source

All Articles