【问题标题】:combining two filter functions结合两个过滤器功能
【发布时间】:2021-03-31 14:51:19
【问题描述】:

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

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

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

【问题讨论】:

  • 你能发布一个可重现的代码示例吗?
  • 您能否提供示例来源和预期结果(可能是预期结果的多个示例)。
  • 不清楚“and in one row”是什么意思 - 听起来你想要merge = [ a, b ],但很不清楚。
  • 解释你期望合并完成什么。它不会将条件合并为一个,甚至不会合并过滤器调用。你想解决什么更高层次的问题?
  • 从字面上看问题标题“结合两个过滤器”,会给你$("table td").filter(function(idx) { return idx &gt;= number1 &amp;&amp; idx &lt; number2 });,但怀疑这就是你所追求的。

标签: javascript jquery filter


【解决方案1】:

你最好使用.slice 方法而不是过滤器,就像这样:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    相关资源
    最近更新 更多