我需要评估一个对象是否为空。
例如假设我有一个对象:
var home = ....; // this is an object and after I print it
console.log(home) // the result of this option is []
//now I need to check if a object is empty or null
if( home == null || home == undefined )
{
// I don't know specify how object is empty
}
如何检测上述对象 home 是否为 null/空?
最佳答案
检查空数组
if (!arr.length) {
// if the array is empty
}
对于一个对象(检查它是否没有键)
if (!Object.keys(obj).length) {
// if the object is empty
}
关于javascript - 如何检查一个对象是否为空? [javascript],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35919611/