filter vs map reactjs and jsx

后端 未结 3 572
慢半拍i
慢半拍i 2021-01-06 15:45

I\'m working on a react project to learn react.

In a component\'s render method, when I use .map to iterate over values and return an array of component

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-06 16:09

    If you only want to use one pass over the array you can use reduce:

    books && books
    .reduce(
      (all,book, index) => {
        if (book.shelf !== shelf) {
          return all;
        }
        return all.concat(
          
        );
      }
      ,[]
    )
    

    However; I think using filter and map makes for code that's easier to read.

提交回复
热议问题