简体   繁体   English

React array map inside map 值不渲染

[英]React array map inside map value not rendering

I have an object Array list and this array list includes duplicate values.我有一个 object 数组列表,这个数组列表包含重复值。 So I need to render only unique values.所以我只需要呈现唯一值。 But it's not working.但它不起作用。

{promotions.map((row) => (
  <div>
    <h1>{row.name}</h1>  //This is working
    {row.products.map(() => {
      const pp = row.products.filter( (ele, ind) => ind === row.products.findIndex( elem => elem.productId === ele.productId))

      pp.map((data)=>{
        return(
          <Chip
            key={`${row.productId}_${data.productId}`}
            classes={{ root: classes.productsChipRoot }}
            label={data.productName}
            style={{ margin: 3, backgroundColor: '#BBD7FB' }}
          /> 
        )
      })
    })}
  </div>
))}

In Render, Map works for just outter map. So, the Inner map is not render.在 Render 中,Map 仅适用于外部 map。因此,内部 map 未渲染。 You should not use the inner map.你不应该使用内部 map。

I recommend using filter method.我建议使用过滤方法。 Like this像这样

row.products.filter(*condition*).map(()=>{
      return <div></div>
)

You need to return your "pp" returned value correctly.您需要正确返回“pp”返回值。

For example:例如:

let firstArray = [{
  id: 1,
  name: "asd"
}, {
  id: 2,
  name: "fgh"
}];
let secondArray = [{
  id: 1,
  name: "hjk"
}, {
  id: 2,
  name: "zxc"
}];

firstArray.map(() => {
  const pp = firstArray.filter((ele, ind) => ind === firstArray.findIndex(elem => elem.id === 2))

  return pp.map((data) => {
    return (
      console.log(data)
    )
  })
})
{promotions.map((row) => (
      <div>
        <h1>{row.name}</h1>  //This is working
        {row.products.map(() => {
          const pp = row.products.filter( (ele, ind) => ind === row.products.findIndex( elem => elem.productId === ele.productId))
    
          return pp.map((data)=>{
            return(
              <Chip
                key={`${row.productId}_${data.productId}`}
                classes={{ root: classes.productsChipRoot }}
                label={data.productName}
                style={{ margin: 3, backgroundColor: '#BBD7FB' }}
              /> 
            )
          })
        })}
      </div>
    ))}

You can do more easily if use Set constructor如果使用Set构造函数,您可以更轻松地完成

 let array = ['A', 'B', 'A', 'C', 'B']; let uniqArray= [...new Set(array )]; console.log(uniqArray);

 {promotions.map((row) => (
    <dev>
       <h1>{row.name}</h1>  //This is working
{row.products.map(() => {
          let tmpArray = []
          row.products.map(pro => {
             if(tmpArray.filter(p => p.productId === pro.productId).length === 0){
                tmpArray.push(pro)
             }
          })
  
          tmpArray.map((data)=>{
           return(
                            <Chip
                              key={`${row.productId}_${data.productId}`}
                              classes={{ root: classes.productsChipRoot }}
                              label={data.productName}
                              style={{ margin: 3, backgroundColor: '#BBD7FB' }}
                            /> 
                             
                          )
                        })
                      })}
   </div>

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

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