2012-01-07 147 views
6

鑑於itemarray,我想知道item是否存在array如何檢查jQuery對象是否存在於數組中?

item是一個jQuery對象,例如, $(".c")。你可以假設item.length == 1

array是一個jQuery對象數組,例如, [$(".a"), $(".b")]。此數組中的每個項目可能表示0,1個或更多個對象。

這裏是我認爲實現這個:(live demo here

function inArray(item, arr) { 
    for (var i = 0; i < arr.length; i++) { 
     var items = $.makeArray(arr[i]); 

     for (var k = 0; k < items.length; k++) { 
      if (items[k] == item[0]) { 
       return true; 
      } 
     } 
    } 

    return false; 
} 

你能找到一個更優雅的實現?


例子:

HTML:

<div class="a">Hello</div> 
<div class="a">Stack</div> 
<div class="a">Overflow</div> 

<div class="b">Have</div> 
<div class="b">a</div> 
<div class="b">nice</div> 
<div class="b">day!</div> 

<div class="c">Bye bye</div> 

JS:

console.log(inArray($(".a").eq(2), [$(".a"), $(".b")])); // true 
console.log(inArray($(".b").eq(3), [$(".a"), $(".b")])); // true 
console.log(inArray($(".c"), [$(".a"), $(".b")]));  // false 
console.log(inArray($(".a").eq(2), [$(".b")]));   // false 
console.log(inArray($(".a").eq(2), []));     // false 
console.log(inArray($(".c"), [$("div")]));    // true 
+2

是否必須是一個數組?你爲什麼不使用jQuery對象和['.index()'](http://api.jquery.com/index/)? – 2012-01-07 10:37:18

+0

@Felix:我想你的意思是使用'$(「.a,.b」)'。聽起來很合理! – 2012-01-07 10:48:34

+1

或者你可以使用['add()'](http://api.jquery.com/add/)構建jQuery對象。 – 2012-01-07 10:57:27

回答

9

據Felix的建議:

[$(selector1), $(selector2), ... ]可以簡化爲

$(selector1, selector2, ...) 

$(selector1).add(selector2)... 

,然後它可以實現爲:

function inArray(item, arr) { 
    return (arr.index(item) != -1); 
} 

Live demo here

+0

太棒了!謝謝! – 2016-03-02 02:50:28

+0

好的解決方案!但我不認爲這真的需要一個函數包裝器 - 儘管OP是在它自己的函數中,只是在函數內部做一行就足夠了。 – Leith 2017-04-10 01:13:24

8

怎麼樣

if(jQuery.inArray(some, array) === -1) 
{ 
//process data if "some" is not in array 
} 
else 
{ 
//process if "some" is in array 
} 

在這裏閱讀:http://api.jquery.com/jQuery.inArray/

+5

雖然upvoted多次,這不適用於包含jQuery對象的數組(這是OP所要求的)。使用jQuery的.index()方法,查看Misha自己的答案,找到正確的解決方案。 – Jpsy 2013-10-25 09:51:27

+0

此解決方案不適用於jquery元素數組 – user590849 2015-02-25 15:30:23

+0

不適用於對象數組。先閱讀。 – Craig 2016-07-20 18:30:16

0
if($.inArray("valueYouWantToFind", nameOfTheArray) == true) { 
    Your code; 
} 

Eg., 
var userChoice = ["yes"]; 
if($.inArray('yes',userChoice) == true) { 
        alert("found"); 
       } 
0
console.log(!!~$.inArray("a", ["a", "b", "c"])); 
+6

你能詳細說一下嗎?「試試這個」風格的答案不能解釋太多,並鼓勵同樣的問題反覆提問。 – Gary 2014-10-07 21:28:00

+0

同意上面的評論:詳細說明爲什麼這是一個答案會幫助每個人,尤其是當你在一行中使用兩個JavaScript的「技巧」時。 – Whymarrh 2014-10-07 22:55:26

-1
data = [ 
    {val:'xxx',txt:'yyy'}, 
    {val:'yyy',txt:'aaa'}, 
    {val:'bbb',txt:'ccc'} 
]; 

      var dummyArray = []; 
      var distinctValueArray = []; 

      $.each(data, function (index, firstobject) { 
       //push first element of object in both dummy array and distinct array. 

       if (index == 0) { 
        distinctValueArray.push(firstobject); 
        dummyArray.push(firstobject.txt); 
       } 
       else { 

        //started from 2nd index. 

        if ($.inArray(firstobject.txt, dummyArray) == -1) { 
         distinctValueArray.push(firstobject); 
        } 
        dummyArray.push(firstobject.txt); 
       } 
      }); 
      dummyArray.length=0; 
相關問題