everycode icon indicating copy to clipboard operation
everycode copied to clipboard

2015年3月2日

Open nunnly opened this issue 9 years ago • 6 comments

/* Param Object
 * return true or false
 * 传入一个对象,如果对象的属性值为空,那么返回true,如果不为空,返回false
 * var a = {};
 * isEmpty(a);//should return true;
/
function isEmpty(obj){

}

nunnly avatar Mar 02 '15 01:03 nunnly

function isEmpty(obj){
    return !Object.keys(obj).length;
};

// 测试用例
var a = {a:1};
console.log(isEmpty(a));

var a = {};
console.log(isEmpty(a));

嘻嘻

think2011 avatar Mar 02 '15 01:03 think2011

function isEmpty(obj) {
if (Object.keys) {
    return !Object.keys(obj).length;
} else {
    for (var p in obj) {
        if (obj.hasOwnProperty(p)) {
            return true;
        }
    }
}

return false;
}

lzzwoodtree avatar Mar 02 '15 02:03 lzzwoodtree

function isEmpty(obj){ for (var p in obj) { return false; } return true; }

cuining avatar Mar 02 '15 07:03 cuining

我写的跟一楼的一模一样。。。汗,不过他比我简洁

function isEmpty(obj){
       return Object.keys(obj).length>0?!1:!0
     }

VaJoy avatar Mar 02 '15 09:03 VaJoy

和上面几位想的一样

function isEmpty(obj) {
    return !Object.keys(obj).length;
}

soulcm avatar Mar 05 '15 02:03 soulcm

天真的我还用in去遍历属性orz 参考前面几楼

function isEmpty(obj){
    return !Object.keys(obj).length;
};

Xinuy-Leung avatar Jul 05 '15 14:07 Xinuy-Leung