admin管理员组文章数量:1288055
I use Jquery to check if my object from an ajax call is empty or not.
In this example I have made a correct AJAX call and it returns some data.
console.log ("obj before Json parse: ",response);
var test = $.isEmptyObject(response);
console.log("test if object is empty:",test);
obj before Json parse: [{"dateTime":"2015-10-02","entries":220}]
est if object is empty: false
However in this example I have made an incorrect AJAX call that returns nothing.
console.log ("obj before Json parse: ",response);
var test = $.isEmptyObject(response);
console.log("test if object is empty:",test);
obj before Json parse: []
test if object is empty: false
surely the test variable should be true in this case as the object is empty?
I use Jquery to check if my object from an ajax call is empty or not.
In this example I have made a correct AJAX call and it returns some data.
console.log ("obj before Json parse: ",response);
var test = $.isEmptyObject(response);
console.log("test if object is empty:",test);
obj before Json parse: [{"dateTime":"2015-10-02","entries":220}]
est if object is empty: false
However in this example I have made an incorrect AJAX call that returns nothing.
console.log ("obj before Json parse: ",response);
var test = $.isEmptyObject(response);
console.log("test if object is empty:",test);
obj before Json parse: []
test if object is empty: false
surely the test variable should be true in this case as the object is empty?
Share Improve this question asked Oct 12, 2015 at 10:52 Andreas Uldall LeonhardAndreas Uldall Leonhard 3151 gold badge3 silver badges11 bronze badges 2- 1 Possible duplicate of How do I test for an empty Javascript object? – Diptox Commented Oct 12, 2015 at 10:54
-
1
isEmptyObject
should only be used on plain objects, you seem to have an array, and could just doresponse.length
instead. – adeneo Commented Oct 12, 2015 at 10:54
2 Answers
Reset to default 7Use length
to check if the object is empty or not.
var isEmpty = (response || []).length === 0;
var jsonData = JSON.parse(responseBody);
tests['empty_or_not'] = jsonData.length === 0;
本文标签: javascriptChecking if Json object is emptyStack Overflow
版权声明:本文标题:javascript - Checking if Json object is empty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741333624a2372910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论