javascript - Determine a double array variable: (does not exist or is empty or [[]]) == true?
巴扎黑
巴扎黑 2017-05-18 11:01:34
0
3
400

As in the title:

var doubleArray ;
if(!doubleArray || (doubleArray.lenght==&& doubleArray[0].length == 0 ){
return;
}
巴扎黑
巴扎黑

reply all(3)
滿天的星座
var isArray = function(o) {
    return Object.prototype.toString.call(o) === '[object Array]';
}

var doubleArray ;
if(!isArray(doubleArray) || !isArray(doubleArray[0]) || !doubleArray[0].length ){
    return;
}
我想大声告诉你

Not exactly equivalent. . . . . . It is equivalent if you limit doubleArray to be an array within double. . .

undefined != true
null != true

It’s all normal, for the object==其实是toPrimitive之后再进行比较。
对于Array,其实就是toString之后在进行比较,toString默认调用join
所以不论几重的数组,只要是空的,都是相当与“” == true

phpcn_u1582

Combine the situation yourself. Just write it

function isEmpty(obj) {
    if (!obj) {
        return true;
    }
    if (Array.isArray(obj)) {
        if (!obj.length) {
            return true;
        } else if (obj.length === 1 && Array.isArray(obj[0]) && obj[0].length === 0) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }


}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template