使用 JavaScript/jQuery 检查对象是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13973927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if Object is empty with JavaScript/jQuery
提问by Artpixler
I have an object like this:
我有一个这样的对象:
ricHistory = {
name1: [{
test1: value1,
test2: value2,
test3: value3
}],
name2: [{
test1: value1,
test2: value2,
test3: value3
}]
};
Now I want to check if e.g. name2 is empty with Javascript/jQuery. I know the method hasOwnProperty. It work for data.hasOwnProperty('name2')only if the name exists or not, but I have to check if its empty.
现在我想用 Javascript/jQuery 检查例如 name2 是否为空。我知道方法hasOwnProperty。它data.hasOwnProperty('name2')仅适用于名称是否存在,但我必须检查它是否为空。
采纳答案by Minko Gechev
Try this:
尝试这个:
if (ricHistory.name2 &&
ricHistory.name2 instanceof Array &&
!ricHistory.name2.length) {
console.log('name2 is empty array');
} else {
console.log('name2 does not exists or is not an empty array.');
}
The solution above will show you whether richHistory.name2 exists, is an array and it's not empty.
上面的解决方案会告诉你richHistory.name2是否存在,是一个数组并且它不是空的。
回答by NullPoiиteя
you can do this by jQuery.isEmptyObject()
你可以这样做 jQuery.isEmptyObject()
Check to see if an object is empty (contains no properties).
检查对象是否为空(不包含任何属性)。
jQuery.isEmptyObject( object )
Example:
例子:
jQuery.isEmptyObject({}) // true
jQuery.isEmptyObject({ foo: "bar" }) // false
回答by burakakkor
Another syntax from JQuery which is you are not using Prototype or similar and you prefer to use $ rather than jQuery prefix;
来自 JQuery 的另一种语法是你没有使用 Prototype 或类似的,你更喜欢使用 $ 而不是 jQuery 前缀;
$.isEmptyObject( object )
回答by stamat
Try this useful function:
试试这个有用的功能:
function isEmpty(obj) {
if(isSet(obj)) {
if (obj.length && obj.length > 0) {
return false;
}
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
return false;
}
}
}
return true;
};
function isSet(val) {
if ((val != undefined) && (val != null)){
return true;
}
return false;
};
回答by Fokwa Best
I go like this all the time in my code:
我在我的代码中一直是这样的:
Assume my controller returns something like below:
假设我的控制器返回如下内容:
$return['divostar'] = $this->report->get_additional_divostar_report($data);
$return['success'] = true;
return response()->json($return);
In Jquery, I would check like below:
在 Jquery 中,我会检查如下:
if (jQuery.isEmptyObject(data.divostar)){
html_result = "<p id='report'>No report yet</p>";
$('#no_report_table').html(html_result).fadeIn('fast');
}
回答by elixon
Dirty but simple and works:
肮脏但简单且有效:
function isEmptyObject(obj) {
return JSON.stringify(obj) == '{}';
}
回答by Rogala
You could shorten isSetby doing this instead:
你可以isSet通过这样做来缩短:
function isSet(val) { return ((val != undefined) && (val != null));}
I find it easier to just return the boolean of the statement instead of handing and then returning.
我发现只返回语句的布尔值更容易,而不是处理然后返回。
回答by Per Salbark
if (ricHistory.name2 === undefined) {
//This is property has not been set (which is not really the same as empty though...)
}

