【问题标题】:reduce(prev , curr) function return undefined in react reduxreduce(prev,curr)函数在react redux中返回未定义
【发布时间】:2019-04-28 06:09:43
【问题描述】:

brands = ["nike", "caprese", "lifestyle", "puma"]

products = {name: "fullname", tags: ["caprese", "lifestyle"]}

return products.filter(product => {
   return brand.reduce((prev , curr) => {
       if (tags.includes(curr) > -1) {
            return prev && true;
        }
   },true);
})

我想过滤带有品牌的产品,但它返回未定义,第一次工作但一旦我更改产品标签,它就会返回未定义

【问题讨论】:

  • products 是一个对象,您不能在对象上使用filter,因为它需要一个集合。
  • 是的,我知道而且我有产品收藏
  • 产品 =[ {name: "fullname", tags: ["caprese", "lifestyle"]}, name: "fullname", tags: ["caprese", "lifestyle"]},名称:“全名”,标签:[“caprese”,“生活方式”]}]

标签: reactjs filter redux include reduce


【解决方案1】:

尝试以下方法:

return products.filter(({ tags }) => tags.some(tag => brands.includes(tag));

如果条件至少对一个元素成立,some 函数将返回 true。

【讨论】:

  • 实际上我这里有多个过滤器 export const getVisibleproducts = (data, { brand, value, sortBy, startPrice, endPrice }) => { return data.products.filter(product => { return brand. reduce((prev , curr) => { if (product.tags.includes(curr) > -1) { return prev && true; } }, true); const startPriceMatch = typeof value.min !== 'number' | | value.min
  • 好的,我的意思是我有多个过滤器。所以我想返回 true,所以该项目只显示前端。
  • 非常感谢,效果很好。
猜你喜欢
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2021-10-22
  • 2021-08-06
  • 2019-02-05
  • 2019-05-17
  • 2018-10-14
相关资源
最近更新 更多