Javascript 使用array.prototype.filter()方法根据另一个数组的参数进行筛选
我有一个对象数组,要根据渲染复选框数组中的值对其进行过滤。(即:如果选中了5个复选框中的2个,我希望通过这些复选框指定的参数过滤数据 我已经能够做一个快速而肮脏的解决方案版本,其中我硬编码了复选框数组的所有预期值,但我认为这确实是不必要的Javascript 使用array.prototype.filter()方法根据另一个数组的参数进行筛选,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我有一个对象数组,要根据渲染复选框数组中的值对其进行过滤。(即:如果选中了5个复选框中的2个,我希望通过这些复选框指定的参数过滤数据 我已经能够做一个快速而肮脏的解决方案版本,其中我硬编码了复选框数组的所有预期值,但我认为这确实是不必要的 // Filter variable (array of objects) const filterValues = [ { "id": 1, "min": 0, "max": 5, "
// Filter variable (array of objects)
const filterValues = [
{
"id": 1,
"min": 0,
"max": 5,
"checked": true
}..... ,
]
我试图在filterData中使用一些高阶函数的组合来映射参数列表,然后.filter()可以使用这些参数来正确过滤数据
非常感谢您的帮助您可以使用内置的过滤器
如下:
const filterData = (dataList, filterValues) =>
dataList.filter(item =>
filterValues.some(f => f.checked &&
(f.max >= item.length) &&
(item.length > f.min)
)
)
您可以像这样使用inside过滤器
:
const filterData = (dataList, filterValues) =>
dataList.filter(item =>
filterValues.some(f => f.checked &&
(f.max >= item.length) &&
(item.length > f.min)
)
)
这将有助于
https://codepen.io/piotrek/pen/mXpRmQ?editors=1010
这将有助于https://codepen.io/piotrek/pen/mXpRmQ?editors=1010