简体   繁体   中英

How to display data from object array inside map without duplicates in reactjs

need some help in reactjs

i have this data below and i want to display it wihout duplicate, meaning, it should be a single category for "A", "B" and "D", i want to do this inside the map below.

 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" },
   ]
 };


person.con && person.con.map((data, index) => ( 
  console.log(data, 'data')   
  // I want to display results here...
 
))

expected 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" },

is this possible? Thank you.

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' } ]

I think it should look kind of like this: restructure your data a bit to make it easier to render and then render it.

const person = {
  con: [
    { category: "A", name: "john" },
    { category: "A", name: "doe" },
    { category: "B", name: "rahul" },
    { category: "B", name: "jay" },
    { category: "C", name: "dave" },
    { category: "D", name: "alex" },
    { category: "D", name: "devid" },
    { category: "E", name: "sam" },
    { category: "F", name: "sam" },
    { category: "G", name: "sam" }
  ]
};

const groupedByCategory = person.con.reduce((acc, item) => {
  if (!Array.isArray(acc[item.category])) {
    acc[item.category] = [];
  }
  acc[item.category].push(item.name);
  return acc;
}, {});

const Comp = () => {
  return Object.entries(groupedByCategory).map(([category, names]) => {
    return (
      <div>
        <h3>{category}</h3>
        <ul>
          {names.map((name) => (
            <li>{name}</li>
          ))}
        </ul>
      </div>
    );
  });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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