检查数组值是否为空
问题描述:
我试图检查数组beda[i].person.name
的值是否为为空。但它一直忽略它。我想不出我在做什么错如果声明检查数组值是否为空
$.ajax({
url: link,
method: 'GET',
success: function (beda) {
console.log(beda);
for(var i = 0; i < beda.length; i++){
$("ul").first().append("<li>" + beda[i].person.name + " ... " + beda[i].character.name + "</li>");
console.log(beda[i].person.name)
if (beda[i].person.name == '' || beda[i].person.name == null || beda[i].person.name == undefined || beda[i].person.name.lenght == 0) {
$("ul").first().append("<li>" + "No names found!" + "</li>");
}
}
}
});
答
的问题是因为你有一个错字。 lenght
应该是length
。
另外请注意,您可以使用类型强制转换简化if
声明:
if (!beda[i].person.name) {
$("ul").first().append("<li>" + "No names found!" + "</li>");
}
+0
我试过你的解决方案,但它仍然不能正常工作。 – Slasher
+0
尝试检查控制台是否有错误 –
'的console.log(贝达[I] .person.name)'你会明白什么在 – Coderaemon
去,我已经有这样的在我的代码中。 – Slasher