0

Is there any way to know if a data is repeated within a map? For example:

 newArray = [name: "Jose", name:"Pedro", name:"Jose", name:"Ramon"]

 newArray.map((questmapn: any, index: any) => ({questmapn.name}))

I need to know if questmapn.name is repeated inside the loop to create a ternary that doesn't show the duplicates. Is there a simplified way?

rafa_pe
  • 155
  • 1
  • 4
  • 15
  • Does this answer your question? [Removing duplicate objects](https://stackoverflow.com/questions/18590076/removing-duplicate-objects) – Amila Senadheera Jul 01 '22 at 16:12

1 Answers1

0

You can use onlyUnique function like below:

const newArray = [
  { name: "Jose" },
  { name: "Pedro" },
  { name: "Jose" },
  { name: "Ramon" }
];

function onlyUnique(repeatedArray) {
  const names = [];
  const uniqueArray = [];
  repeatedArray.forEach((item) => {
    if (!names.includes(item.name)) {
      names.push(item.name);
      uniqueArray.push(item);
    }
  });

  return uniqueArray;
}

const uniqueArray = onlyUnique(newArray);

Note: Pass repeated array to this function and returned value will be unique based on name property.