3

How do I write a conditional statement for a jquery $.each loop? I tried to follow the suggestions at Is there conditional looping 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 that, 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 also tried a variety of different combinations of brackets, quotes, and the like, but I haven't found a solution.

Community
  • 1
  • 1
user3080392
  • 1,134
  • 5
  • 17
  • 33

1 Answers1

3

try the following

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

or

if (pType === "Album")

using just a single '=' will set the value, using '===' will check for congruency

Liam Schauerman
  • 836
  • 5
  • 9