如何将变量传递给匹配函数javascript?

科技总动员

在这里,我有 handleFilterType 函数,它动态返回一个字符串并将返回值放入 filters_value 变量中。

现在我需要将这个filtered_value 变量传递给我的匹配函数。那么有什么方法可以传递变量来匹配函数。我尝试了最多,但找不到解决方案。

const filtered_value = this.handleFilterType();  // function return string and set into filter_value variable

const getFilterDefaultValues={({ types }) => {
            const match = types.find(type => type.name.match(/filtered_value/i)); //need to pass filtered_value variable into match function
            return match;
      }}
塞巴斯蒂安·卡茨马雷克

你可以只传递一个RegExp对象:

type.name.match(new RegExp(filtered_value, 'i'));

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章