jQuery / javascript find values within a list -
i have peculiar issue i'm dealing with. i'll start off vague , if needs more detail can give background on project.
i have selected id (selected checkbox): 161
and have many rows of ids so:
["161", "165", "573", "190", "150", "283"] // 1 ["160", "311", "793", "309", "301"] ["161", "165", "395", "306"] // 1 ["160", "311", "668", "191", "216", "301"]
i need identify out of rows of ids above, ones have id has been selected. isn't difficult, can loop through each row of ids (looping through actual array) , thisidlist[i] == selectedid
.
problem i'm having when more 1 id has been selected: ["161", "306"]
now need loop through rows , identify rows has both of selected ids.
["161", "165", "573", "190", "150", "283"] // wouldn't ["160", "311", "793", "309", "301"] ["161", "165", "395", "306"] // 1 ["160", "311", "668", "191", "216", "301"]
and on. there can anywhere 1 5 or 6 ids selected: ["161", "306", "216", "668"]
can point me in right direction? think comparing 2 lists each item in list needs found in list b.
edit
i should add row can contain other ids not found in selected list. if selected ids ["161", "306"]
, ["161", "165", "395", "306"]
match still, though contains 165 , 395.
edit
going update , give bit more info. have list of radio buttons:
<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" />
i have unordered list, each list has data attribute (i'm using metadata plugin):
<ul> <li data="{attrid: '160,197,161,194,195,190,162' }">lorem ipsum</li> </ul>
when radio button clicked:
// set selected ids selectedids = []; $("input[name=filter]:checked").each(function(){ selectedids.push(this.value); }); // loop through each list $('ul li').each(function () { // , set metadata attrid key meta = $(this).metadata(); idlist = meta.attrid; // find out if selected ids found in idlist var ismatch = matches(selectedids,idlist); console.log(ismatch); // plan if(ismatch){ // list item } });
this uses inarray
function jquery. returns array containing indices of sets contain elements of target set. if sets relatively small, in example, should fast enough.
function matches( target, sets ) { var matches= []; (var = 0, setslen = sets.length; < setslen; ++i ) { if (issubset(target,sets[i])) { matches.push(i); } } return matches; } function issubset( target, set ) { (var j = 0, targetlen = target.length; j < targetlen; ++j) { if ($.inarray(target[j], set) < 0) { return false; } } return true; }
a little test script based on data:
$(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 ) ); });
edit: updated example based on additions. think you'd need use issubset function. i'll leave rest of answer context.
Comments
Post a Comment