-2

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.

ZachDLR
  • 11
  • 4
  • category, sorry will edit my question. regardless of the name, the category should display only category (should be 1 A only) – ZachDLR Nov 07 '20 at 05:47
  • Can you update the question with the expected output ? – Guruparan Giritharan Nov 07 '20 at 05:48
  • 1
    Does this answer your question? [Remove duplicate values from JS array](https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array) – Guruparan Giritharan Nov 07 '20 at 05:50
  • @GuruparanGiritharan, thing is, it is object inside an array and i want to display it inside the ".map" as shown above – ZachDLR Nov 07 '20 at 05:52
  • Create a variable or state and keep the tracking of printed category on that variable in array form , and match the current value under your iteration with created variable. – Jitendra Yadav Nov 07 '20 at 05:55

2 Answers2

0

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' } ]
Abishek Kumar
  • 519
  • 5
  • 13
0

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>
    );
  });
};
Sergey
  • 1,000
  • 8
  • 19