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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With