如何为 jquery $.each 循环编写条件语句?我尝试按照 Is there conditional looping with $.each function in jQuery 上的建议进行操作和其他帖子,但是,我无法让它工作:
$.each((rgJson1["release-groups"]), function(index, item) {
var workTitle = item.title;
var pType = item['primary-type'];
var frDate = item['first-release-date'];
if (pType = "Album") {
console.log(workTitle);
}
});
当我尝试这样做时,它将 pType 变量设置为“Album”。所以,我尝试了:
if (item['primary-type'] = "Album") {
console.log(item.title);
但是还是不行。我也尝试了括号、引号等各种不同的组合,但我没有找到解决方案。
最佳答案
尝试以下操作
if (item['primary-type'] === "Album")
或
if (pType === "Album")
仅使用一个“=”将设置值,使用“===”将检查是否一致
关于javascript - jquery循环条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559448/