1

I have an object Array list and this array list includes duplicate values. 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>
))}
1
  • 2
    You need to return the result of pp.map. E.g.: return pp.map(... And potentially also flatten the result of your first map call. E.g. row.products.map(...).flat() Commented Feb 8, 2022 at 6:25

5 Answers 5

3

In Render, Map works for just outter map. So, the Inner map is not render. You should not use the inner map.

I recommend using filter method. Like this

row.products.filter(*condition*).map(()=>{
      return <div></div>
)
Sign up to request clarification or add additional context in comments.

1 Comment

You’re welcome :D
1

You need to return your "pp" returned value correctly.

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)
    )
  })
})

Comments

0
{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>
    ))}

Comments

0

You can do more easily if use Set constructor

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

console.log(uniqArray);

1 Comment

This doesn't answer the question. The problem is that the results aren't rendering, not that they aren't unique. Also, Set() on an array of objects will not remove any duplicates because all of the objects are unique.
-1
 {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>

2 Comments

This won't work. It has the same problem as the code in the question. Also, there's no need to nest a filter inside a map. Filter should work on its own.
Can you please provide your sample array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.