简体   繁体   English

结合两个过滤器功能

[英]combining two filter functions

I'd like to combine two filter functions I'm using to select a few elements in a table.我想将我正在使用的两个过滤器函数组合到 select 表格中的几个元素。 my code looks like this:我的代码如下所示:

a = $('table td').filter(function(index) {
    return index >= number1
}); 
                        
b = $('table td').filter(function(index) {
    return index < number2
});
                        
merge = $.merge(a, b);

The elements from a have to be first and in one row. a中的元素必须是第一个并且在一行中。 so if a returns 3 elements they have to be in one row, follow by the elements from b .因此,如果a返回 3 个元素,它们必须在一行中,然后是b中的元素。 how can I achieve that?我怎样才能做到这一点? can I combine the filter functions from above?我可以结合上面的过滤器功能吗?

you better use .slice method instead of filters, in a way like:您最好使用.slice方法而不是过滤器,例如:

list = $('table td')
merge = [...list.slice(number1), ... list.slice(0, number2)]

or if you really want to use filter then:或者如果你真的想使用过滤器,那么:

list = $('table td')
merge = list.filter((item, i) => (i >= number1 || i < number2)  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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