繁体   English   中英

如何将.filter的输出存储在数组或字符串中?

[英]How can I store the output of .filter in an array or string?

如何将filter()函数的输出存储在数组或字符串中,以便以后在另一个函数中使用?

jQuery('.marca').change(function(){
  var regex = new RegExp(/^2x\d$/);

  if(jQuery(this).find('input').prop("checked")) {

    jQuery(this).nextAll().filter(function() {

// I would like to save the output from this .filter 
// function to use to make a string of classes to pass to the next function.
      console.log(this.className.match(regex)); //output below
      return this.className.match(regex);
  })    
    .show();
  } else {
    jQuery(this).nextAll().hide();
  }
});

我使用上面的代码来检查带有复选框的表单的类,并且仅在选中上一个按钮时才显示“子类”。 我使用regex和filter()查找和显示下一组类,并且我想在下一个jQuery选择器中传递结果类,以避免手动添加它们。

希望我有道理。 为了更好的理解,这是整个代码的摆弄-https: //jsfiddle.net/srjjj/brtp8x2h/9/

我试图将结果简单地添加到变量中,但它不会存储整个值集,而只存储最后一个值(.2x4)。

这是上面的console.log输出,我想它不起作用的原因是因为这不是一个数组数组,而是4个不同的数组,但是我不确定下一步该怎么做以及如何将它们全部保存到变量中。

console.log输出

您是否尝试过在filter函数之外声明一个数组,然后将值推入该数组中?

var matched = [];
jQuery(this).nextAll().filter(function () {
    matched.push(yourFilteredElement);
});

暂无
暂无

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

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