search

Home  >  Q&A  >  body text

javascript - Determine a double array variable: (does not exist or is empty or [[]]) == true?

As in the title:

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

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-18 11:03:34

    var isArray = function(o) {
        return Object.prototype.toString.call(o) === '[object Array]';
    }
    
    var doubleArray ;
    if(!isArray(doubleArray) || !isArray(doubleArray[0]) || !doubleArray[0].length ){
        return;
    }

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-18 11:03:34

    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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-18 11:03:34

    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;
        }
    
    
    }

    reply
    0
  • Cancelreply