首页 > 解决方案 > 结合两个过滤器功能

问题描述

我想结合使用两个过滤器函数来选择表格中的一些元素。我的代码如下所示:

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

from 的元素a必须是第一个并且在一行中。因此,如果a返回 3 个元素,它们必须在一行中,然后是b. 我怎样才能做到这一点?我可以结合上面的过滤器功能吗?

标签: javascriptjqueryfilter

解决方案


您最好使用.slice方法而不是过滤器,例如:

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

或者如果你真的想使用过滤器,那么:

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

推荐阅读