Javascript Data Type How to - Check undefined, null, false value in array recursively








Question

We would like to know how to check undefined, null, false value in array recursively.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--   www  . ja va 2 s  . c  om-->
function xul(func, loc, arr){
    var elem;
    var props = {};
    for (var i = 0, len = arr.length; i < len; i++){
        if (arr[i] == undefined) {
            document.writeln("undefined" + " - " + len);
            document.writeln('<br/>');
        }
        else if (arr[i] == null) {
            document.writeln("null" + " - " + len);
            document.writeln('<br/>');
        }
        else if (arr[i] == false) {
            document.writeln("false" + " - " + len);
            document.writeln('<br/>');
        }
        else if (typeof arr[i] == "string"){
            elem = arr[i];
            if (typeof arr[i + 1] == "object") {
                props = arr[i+1];
                i++;
            }
            loc = createNode(func, loc, elem, props);   
        }
        if (typeof arr[i + 1] == "array") {
            xul("append", loc, arr[i+1]);
        } else {
            return loc;
        }   
    }
}
var array2=[undefined,undefined]
var array = [null,array2 ,1,2,3,4,'asdf', false];
document.writeln(xul(1,2,array));

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: