Jquery loop conditional expression

How do I write a conditional statement for a jquery $ .each loop? I tried to follow the guidelines in Is there a conditional loop with $ .each function in jQuery and other posts, however I couldn't get it to work:

$.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);

}

});

      

When I tried it, it set the pType variable to Album. So, I tried:

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

console.log(item.title);

      

But it still didn't work. I've also tried many different combinations of brackets, quotes and the like, but I haven't found a solution.

+3


source to share


1 answer


try the following

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

      

or



if (pType === "Album")

      

using just one '=' will set the value, using '===' will check for a match

+3


source







All Articles