Javascript 数组中的jquery推送条件

Javascript 数组中的jquery推送条件,javascript,jquery,Javascript,Jquery,我被一个代码卡住了不知道为什么它不工作请建议我 我在数组中推条件,就像 var condition = []; if(performer !== "All" ){ condition.push("(($( this ).attr('performer') === $.trim(performer)))"); } if(music !== "All" ){ condition.push("(($( this ).attr('music') === $.trim(music)))")

我被一个代码卡住了不知道为什么它不工作请建议我

我在数组中推条件,就像

var condition = [];
if(performer !== "All" ){
    condition.push("(($( this ).attr('performer') === $.trim(performer)))");
}
if(music !== "All" ){
    condition.push("(($( this ).attr('music') === $.trim(music)))");
}
if(collector !== "All" ){
    condition.push("(($( this ).attr('collector') === $.trim(collector)))");
}
if(clickedThemeArray.toString() !== "All"){
    condition.push("(arraysEqual(themeArray,clickedThemeArray))");
}
console.log(condition.join(" && "));
然后像这样加入&&

var finalCon = condition.join(" && ");
if(finalCon){
    $( this ).addClass('displayBlock');
}
但它不起作用请帮帮我


提前谢谢。

去掉双引号

condition.push("(($( this ).attr('performer') === $.trim(performer)))");
应该是

condition.push($( this ).attr('performer') === $.trim(performer));
等等


否则它们将被计算为字符串而不是布尔值

去掉双引号

condition.push("(($( this ).attr('performer') === $.trim(performer)))");
应该是

condition.push($( this ).attr('performer') === $.trim(performer));
等等


否则,它们将被计算为字符串,而不是布尔值

通过添加这些双引号,将数组中的条件作为字符串推送。因此,当您将它们与
和&
组合使用时,它们不会按预期工作

书写
condition.push($(this.attr('performer')==$.trim(performer))

应该足够了。

通过添加双引号,您正在将数组中的条件作为字符串推送。因此,当您将它们与
和&
组合使用时,它们不会按预期工作

书写
condition.push($(this.attr('performer')==$.trim(performer))

应该足够了。

一种解决方案是保留一个运行变量:

var passedConditions = true;
if(performer !== "All" ){
    passedConditions = passedConditions && $( this ).attr('performer') === $.trim(performer);
}
if(music !== "All" ){
    passedConditions = passedConditions && $( this ).attr('music') === $.trim(music);
}
if(collector !== "All" ){
    passedConditions = passedConditions && $( this ).attr('collector') === $.trim(collector);
}
if(clickedThemeArray.toString() !== "All"){
    passedConditions = passedConditions && arraysEqual(themeArray,clickedThemeArray);
}

if (passedConditions) {
    $( this ).addClass('displayBlock');
}

一种解决方案是保留一个正在运行的变量:

var passedConditions = true;
if(performer !== "All" ){
    passedConditions = passedConditions && $( this ).attr('performer') === $.trim(performer);
}
if(music !== "All" ){
    passedConditions = passedConditions && $( this ).attr('music') === $.trim(music);
}
if(collector !== "All" ){
    passedConditions = passedConditions && $( this ).attr('collector') === $.trim(collector);
}
if(clickedThemeArray.toString() !== "All"){
    passedConditions = passedConditions && arraysEqual(themeArray,clickedThemeArray);
}

if (passedConditions) {
    $( this ).addClass('displayBlock');
}

好的,如果你看整个问题,询问者正在组合他最后推到数组中的字符串,并试图检查结果字符串。当需要使用eval时,会出现这种情况吗?是的,但是当我去掉双引号时,它会给我布尔值,但我需要这些条件来根据先前的if生成最终的if条件conditions@Zack . 我不认为OP试图使用eval进行评估。因为这似乎是一个简单的if条件。@Sushanth——这就是我要说的。sismaster不使用eval,但在计算字符串表达式时,应该使用eval。对,这是一种方法。。。但是不要认为我们在这里需要它,它们可以在运行中进行评估。好吧,如果你看一下整个问题,询问者正在组合他最后推到数组中的字符串,并尝试检查结果字符串。当需要使用eval时,会出现这种情况吗?是的,但是当我去掉双引号时,它会给我布尔值,但我需要这些条件来根据先前的if生成最终的if条件conditions@Zack . 我不认为OP试图使用eval进行评估。因为这似乎是一个简单的if条件。@Sushanth——这就是我要说的。sismaster不使用eval,但在计算字符串表达式时,应该使用eval。对,这是一种方法。。。但不要认为我们在这里需要这些,它们可以随时评估。