本文介绍了检测对象是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检测对象是否有数据,但这似乎不起作用:
I'm trying to detect if an object has data or not but this doesn't seem to work:
if(item.sellers.length != 0) {
完整代码:
.each(response, function(i, item) {
if(item.sellers.length != 0) {
$.each(item.sellers, function(index, value) {
$('#modal-table tbody').append("<tr><td></td><td><strong>Seller:</strong></td><td>" + index + "</td><td>"+ value + "</td>");
});
});
json如下:
response =
{
"5": {
"name": "surgeon bueno",
"country": "Spain",
"antiquity": "renewal",
"amount": "2686.97 USD",
"sellers": {
"Frank": "2690.58 USD",
"Bob": "1690.58 USD",
}
},
"11": {
"name": "Alex Lloyd",
"country": "American Samoa",
"antiquity": "new client",
"amount": "0.0 USD"
},
"12": {
"name": "alex lloyd",
"country": "Aruba",
"antiquity": "new client",
"amount": "0.0 USD"
}
}
推荐答案
使用hasOwnProperty
:
if(item.hasOwnProperty("sellers")) {
// each loop
}
这篇关于检测对象是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!