How to Remove Duplicates Elements Between 2 Arrays in Angular

How to remove duplicate object from an array in angular 6

If addComp is the only place you modify this.item, just check for existing prior to insertion. Duplicates will never get put in the array, so you'll never have to trim them.

addComp(Names,c){
let item = {name: Names, componentid: c};
if (this.item.find((test) => test.name === Names) === undefined) {
this.item.push(item);
}
}

Alternatively, if there are other places that you're modifying this.item, you should be stripping duplicates in a more expected place. Stripping them as a side-effect of the addComp function is unexpected. However, you could do it...

addComp(Names,c){
this.item.push({name: Names, componentid: c});
this.item = this.item.filter((test, index, array) =>
index === array.findIndex((findTest) =>
findTest.name === test.name
)
);
}

Remove duplicate objects from an Array

This one will assign this.subMenuItems an array containing only the first instance of each item because indexOf returns the first index where the object is found.

this.subMenuItems = this.items.filter((item, index, self) => self.indexOf(item) === index);

Remove duplicates while creating an array with multiple properties in TypeScript

See this codepen: https://codepen.io/kyletanders/pen/NWqpWVX?editors=0012

something like this:


const data = [
{name: 'Ana', country: 'US', language: 'EN'},
{name: 'Paul', country: 'UK', language: 'EN'},
{name: 'Luis', country: 'PH', language: 'SP'},
{name: 'Tom', country: 'US', language: 'EN'}
];

let unique = [...new Set(data.map(item => item.country))].map(x => {return {filter: 'Country', value: x}});
console.log(unique);

Removing duplicates data from array using angular 9

Try this,

data = [    [        {            "PTF_NAME": "integration",            "Salle": "salle1"        },        {            "PTF_NAME": "integration",            "Salle": "salle1"        }    ],    [      {            "PTF_NAME": "Qualif",            "Salle": "salle1"      },      {            "PTF_NAME": "Qualif",            "Salle": "salle1"      }    ],    [      {            "PTF_NAME": "Qualif",            "Salle": "salle2"      },      {            "PTF_NAME": "Qualif",            "Salle": "salle2"      }    ],    [{            "PTF_NAME": "Prod",            "Salle": "salle1"        },        {            "PTF_NAME": "Prod",            "Salle": "salle1"        }    ],    [{            "PTF_NAME": "Prod",            "Salle": "salle2"      },        {            "PTF_NAME": "Prod",            "Salle": "salle2"        }    ]];function removeDupesInArrayOfObject() {  const result = [];  result.push(this.data[0]);  data.reduce( (first, second, n) => {      if (first[0].PTF_NAME !== second[0].PTF_NAME) {          result.push(second);      }      return second  })  console.log(result);}removeDupesInArrayOfObject();

Compare two Javascript Arrays and remove Duplicates

array1 = array1.filter(function(val) {
return array2.indexOf(val) == -1;
});

Or, with the availability of ES6:

array1 = array1.filter(val => !array2.includes(val));

filter() reference here

indexOf() reference here

includes() reference here

Compare 2 Arrays of Objects and Remove Duplicates

One option with O(N) complexity would be to make a Set of the ids in cars1, then spread cars1 and a filtered cars2 into the ouput array, with the filter testing whether the id in the car being iterated over in cars2 is included in the Set:

var cars1 = [    {id: 2, make: "Honda", model: "Civic", year: 2001},    {id: 1, make: "Ford",  model: "F150",  year: 2002},    {id: 3, make: "Chevy", model: "Tahoe", year: 2003},];
var cars2 = [ {id: 3, make: "Kia", model: "Optima", year: 2001}, {id: 4, make: "Nissan", model: "Sentra", year: 1982}, {id: 2, make: "Toyota", model: "Corolla", year: 1980},];const cars1IDs = new Set(cars1.map(({ id }) => id));const combined = [ ...cars1, ...cars2.filter(({ id }) => !cars1IDs.has(id))];console.log(combined);

Removing duplicate objects (based on multiple keys) from array

You could use a Set in a closure for filtering.