简体   繁体   English

map 如何在 ReactJs 的 filter() 内抛出 forEach()

[英]How map throw forEach() inside the filter() in ReactJs

Here I have a problem mapping throw forEach() inside the filter(), I'm trying to filter products so for each product object I want to get to the product.categories array , and for each product category I want to check if the product category id === the page category id and then map throw them, and display the product that matches, but nothing gets displayed.在这里,我在 filter() 中映射 throw forEach() 时遇到问题,我正在尝试过滤产品,因此对于每个产品 object我想访问product.categories 数组,并且对于每个产品类别我想检查是否产品类别 id === 页面类别 id然后map抛出它们,并显示匹配的产品,但没有显示任何内容。

 <div> {products.filter(filterProduct => filterProduct.categories.forEach(eachProductCategory => eachProductCategory.id === category.id)).map(product => ( <Product /> </div>

You could try .some instead of forEach您可以尝试.some而不是forEach

<div>
  {products
    .filter((filterProduct) =>
      filterProduct.categories.some(
        (eachProductCategory) => eachProductCategory.id === category.id
      )
    )
    .map((product) => (
      <Product />
    ))}
</div>

forEach doesn't return anything that was the problem forEach不返回任何问题

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

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

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