'how to prevent duplicate data from react

my json object

[
  {
   "id": 1,
   "name": "Banana",
   "category": "Food"
  }
  {
   "id": 2,
   "name": "Milk",
   "category": "Food"
  }
  {
   "id": 3,
   "name": "Phone",
   "category": "Technology"
  }
]

when I map the category names found in my json data, the same category names are constantly repeating. how can I make sure this doesn't happen?

my code=

{card.map((element, i)=>{
<li>{element.category}</li>
})}


Solution 1:[1]

First of all you are missing the return statement.

{
 card.map((element, i) => {
  return <li>{card.category}</li>
 }
}

And second, you are referencing the array card not the element.

{
 card.map((element, i) => {
  return <li>{element.category}</li>
 }
}

Even better:

{
 card.map(el => <li>{el.category}</li>)
}

Solution 2:[2]

You can use Set to filter out duplicates.

const data = [{
  "id": 1,
  "name": "Banana",
  "category": "Food"
}, {
  "id": 2,
  "name": "Milk",
  "category": "Food"
}, {
  "id": 3,
  "name": "Phone",
  "category": "Technology"
}];

const result = [...new Set(data.map(item => {
      return item.category;
    }))];
    
console.log(result);

Solution 3:[3]

import jsonFile from "./file.json"

const List = () => (
  <ul>
    {jsonFile.map(({ id, category }) => (
      <li key={id}>{category}</li>
    ))}
  </ul>
);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BiciAP
Solution 2 kiranvj
Solution 3 poliskalien