简体   繁体   中英

Filtering through a list of JQuery selected items

I am in a situation where I haven't found a selector or a selector function that quite does what I would like it to do.

Therefore I am trying to filter the list to contain only the items I would like it to.

I have a selector

var html = $(".foo .foobar")

This returns what I wanted it to.

Then I have a for loop that loops through those selected items and identifies the ones I want to keep in that list.

However, I need to keep the modified list the same type as a selector so that I can perform jquery actions to them later.

how do I create a copy of the 'html' variable (or a filtered original) but with only the desired rows that were found in the function (Keeping it still in a state as if it was a selector itself)?

Later I have an 'each' loop that begins like this:

html.each(function(i, el) {
   $(this).replaceWith(tempArr[i]);

I am trying to achieve a result where 'html.each' has 'html' as the modified list previously selected.

Thanks.

// Update

 var htmlTemp; for (var primaryCounter = 0, secondryCounter = 0; primaryCounter < htmlTemp.length; primaryCounter++) { if (firstFound) { secondryCounter++; if (secondryCounter % columnCount === 0) { html.push(htmlTemp[primaryCounter]); } } else { if (primaryCounter === currI) { html.push(htmlTemp[primaryCounter]); firstFound = true; } } } 

Above is the function including the logic that I wanted to use (Which isn't going to run). Is there a way with 'filter' possibly where I can call this function and instead of 'push()' just include at these indexes found? Thanks.

Assuming html as an array, you can use html.filter(callbackFunc) to get a new list every time.

Check this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM