2010-12-01 62 views
2

我有一個特殊的問題,我正在處理。我會開始模糊,如果有人需要更多的細節,我可以提供有關該項目的背景知識。jQuery/javascript查找列表中的值

我具有選定的ID(從一個複選框選擇的):161

我有像這樣的ID的許多行:

["161", "165", "573", "190", "150", "283"] // this one would be it 
["160", "311", "793", "309", "301"] 
["161", "165", "395", "306"] // this one would be it 
["160", "311", "668", "191", "216", "301"] 

我需要識別出的ID的上面的行,哪些人的ID已被選中。這並不難,我可以遍歷每行ID(循環遍歷實際數組),並執行thisIDList[i] == selectedID。當已經選擇了多個ID我有

問題是:["161", "306"]

現在我需要通過行循環和識別哪些行具有所選ID的兩個

["161", "165", "573", "190", "150", "283"] // wouldn't be it 
["160", "311", "793", "309", "301"] 
["161", "165", "395", "306"] // this one would be it 
["160", "311", "668", "191", "216", "301"] 

等等。可以選擇1到5或6個ID:["161", "306", "216", "668"]

有人能指出我的方向嗎?我認爲這基本上是比較喜歡的兩個列表,其中A組名單所列每個項目需要在列表B.對發現


編輯

我要補充該行能夠包含在沒有發現其他ID選定的列表。所以,如果所選的ID是["161", "306"],然後["161", "165", "395", "306"]將是一個匹配的是,儘管它包含了165和395.


編輯

要更新,給多一點信息。我有單選按鈕的列表:

<input type="checkbox" name="filter" value="301" /> 
<input type="checkbox" name="filter" value="161" /> 
<input type="checkbox" name="filter" value="573" /> 
<input type="checkbox" name="filter" value="190" /> 

我有一個無序列表,每個列表具有數據屬性(我使用所述元數據插件):

<ul> 
    <li data="{attrID: '160,197,161,194,195,190,162' }">Lorem Ipsum</li> 
</ul> 

當點擊單選按鈕:

// set the selected IDs 
selectedIds = []; 
$("input[name=filter]:checked").each(function(){ 
    selectedIds.push(this.value); 
}); 

// loop through each list     
$('ul li').each(function() { 

    // get and set the metadata for the attrID key 
    meta = $(this).metadata(); 
    idList = meta.attrID; 

    // find out if the selected IDs are found in this idList 
    var isMatch = matches(selectedIds,idList); 

    console.log(isMatch); 

    // my plan was to do this 
    if(isMatch){ 
     // do something with this list item 
    } 


}); 

回答

3

它使用inArray功能從jQuery的。它返回一個數組,其中包含包含目標集所有元素的集合的索引。如果你的套件相對較小,就像在你的例子中那樣,它應該足夠快。根據您的數據

function matches(target, sets) 
{ 
    var matches= []; 
    for (var i = 0, setsLen = sets.length; i < setsLen; ++i) { 
     if (isSubset(target,sets[i])) { 
      matches.push(i); 
     } 
    } 
    return matches; 
} 

function isSubset(target, set) 
{ 
    for (var j = 0, targetLen = target.length; j < targetLen; ++j) { 
     if ($.inArray(target[j], set) < 0) { 
      return false; 
     } 
    } 
    return true; 
} 

一個小的測試腳本:

$(function(){ 
    var sets = [ 
        ["161", "165", "573", "190", "150", "283"], 
        ["160", "311", "793", "309", "301"], 
        ["161", "165", "395", "306"], 
        ["160", "311", "668", "191", "216", "301"] 
    ]; 

    alert(matches([ "161" ], sets)); 
    alert(matches([ "161","306" ], sets)); 
}); 

編輯:我更新根據您的補充我的榜樣。我想你只需要使用isSubset函數。我將把剩下的答案留給上下文。

0

如果您是通過對每個數組進行排序而開始的,則只需遍歷每個列表一次。

0

如何在找到X個ID之一時遞增變量,並在達到最終結果時將其與選定ID的總數進行匹配。如果找到4箇中的4個,則匹配

2

這樣做的最快和最可重用的方法是創建isSubset函數。你甚至不依賴於jQuery!

function isSubset(largeSet, smallSet){ 

    for(var i=0; i<smallSet.length; i++){ 
     if(largeSet.indexOf(smallSet[i]) == -1){ 
      //not found. 
      return false; 
     } 
    } 

    return true; 
} 

現在遍歷所有的ID列表陣列並通過您選擇的ID陣列:

isSubset(idList, selectedIds); 

如果上述返回true,你已經確定了有效列表!

=============

編輯:感謝埃裏克,您指出的indexOf帶一個漏洞()。

下面的代碼應該可以解決所有的瀏覽器這個問題:

if (!Array.indexOf) { 
    Array.prototype.indexOf = function (obj, start) { 
    for (var i = (start || 0); i < this.length; i++) { 
     if (this[i] == obj) { 
     return i; 
     } 
    } 
    return -1; 
    } 
} 
+3

請謹慎[支持IE中的`Array.indexOf()`](http://stellapower.net/content/javascript-support-and-arrayindexof-ie)... – Eric 2010-12-01 20:41:18

+0

謝謝!現在試試這個... – jyoseph 2010-12-01 21:09:55