2014-12-19 318 views
3

如何为jquery $ .each循环编写条件语句?我试图按照在Is there conditional looping with $.each function in jQuery和其他职位的建议,但是,我无法得到它的工作: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); 

} 

}); 

当我想的是,它设置p型变量“专辑”。所以,我试过:

if (item['primary-type'] = "Album") { 

console.log(item.title); 

但它仍然没有工作。我也尝试了各种不同的括号,引号等组合,但我还没有找到解决方案。

回答

3

尝试以下

if (item['primary-type'] === "Album") 

if (pType === "Album") 

只使用一个单一的 '=' 将设置值,使用 '===' 将检查一致性

+0

这真是棒极了。非常感谢。 – user3080392 2014-12-19 04:01:26