2017-03-06 24 views
1

我需要檢查數組元素是否滿足給定的條件。但是我收到「is未定義」錯誤。如何在jquery中的is()中進行條件檢查

var tempfLength = tempfArray.length; 
for(var i=tempfLength-2;i> -1;i--){ 
    alert(tempfArray[i]); 
    if((tempfLength >length)&&(is(tempfArray[i]==parentName))){ 
    $(this).hide('str'); 
    } 
} 
+0

可以有一些更簡單的方法來回答這個問題。但需要更多的上下文。 – Jai

回答

1

更改以下行:

if((tempfLength >length)&&(is(tempfArray[i]==parentName))) 
{ 

} 

if((tempfLength >length) && (tempfArray[i] == parentName)) 
{ 
    // Do something here 
} 

注:

  • 可以使用&&或將多個條件
  • 不能使用==
+0

對我來說似乎是正確的。 +1 –

+0

您可以使用==運算符比較2個對象嗎? – madalinivascu

+0

是的..謝謝你很好.. – Aash

0

比較2個對象is()功能用於像:

if((tempfLength >length)&&(parentName.is(tempfArray[i]))) 
{ 

} 

閱讀文檔:http://api.jquery.com/is/

確保兩個parentName和tempfArray [I]是jquery objects

+0

感謝您的重播,但我認爲在這裏我不能使用此becoz parantName不是一個函數它是可變的。我對麼? – Aash

+0

@Aash你是什麼意思,你如何在另一個函數上調用函數parantName是一個變量,你可以在一個變量上調用'is()'而不是函數 – madalinivascu

+0

是的,但是當我使用這個時, 「Uncaught TypeError:parentName.is不是函數」。並再次感謝你,我剛剛搜索谷歌是()並獲得更多的信息。 – Aash

0

is is not definded.

這個錯誤表示函數is()在您調用它的上下文的全局上下文中不可用。

我的理解,你可以用Array.prototype.find()試試這個問題:

var $this = $(this); // <----make sure to cache it as in the array this doesn't belong to 
var tempfLength = tempfArray.length; //`----what you think. 
for(var i=tempfLength-2;i> -1;i--){ 
    alert(tempfArray[i]); 
    if((tempfLength >length)&&(tempfArray.find(function(item){item === parentName})))){ 
     $this.hide('str'); 
    } 
} 
+0

感謝jai的重播,但它表明它不會工作,再次出現某種錯誤。「Uncaught TypeError:無法讀取未定義的屬性'find'。 – Aash

+0

opps我的錯誤只是忘記了包含函數查找和使用數組。 – Jai

+0

@Aash剛剛更新。 – Jai

相關問題