how do i display data from an object array inside a map in reactjs?

Here a sample code

const person = {
    con: [
     { category: "A", name: "john"  },
     { category: "A", name: "john" },
     { category: "B", name: "rahul" },
     { category: "B", name: "jay" },
     { category: "C", name: "dave" },
     { category: "D", name: "alex" },
     { category: "D", name: "alex" },
     { category: "E", name: "sam1" },
     { category: "F", name: "sam2" },
     { category: "G", name: "sam3" },
    ]
  };
const duplicateCheck = [];
 
person.con && person.con.map((data, index) => {
    if (duplicateCheck.includes(data.category))
        return null;
    duplicateCheck.push(data.category);
    return data;
}).filter((e)=>(e))
// Above code returns filtered out array

Output:

  [ { category: 'A', name: 'john' },
  { category: 'B', name: 'rahul' },
  { category: 'C', name: 'dave' },
  { category: 'D', name: 'alex' },
  { category: 'E', name: 'sam1' },
  { category: 'F', name: 'sam2' },
  { category: 'G', name: 'sam3' } ]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top