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 id
s 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.
const listOfTags = [{ id: 1, label: "Hello", color: "red", sorting: 0 }, { id: 2, label: "World", color: "green", sorting: 1 }, { id: 3, label: "Hello", color: "blue", sorting: 4 }, { id: 4, label: "Sunshine", color: "yellow", sorting: 5 }, { id: 5, label: "Hello", color: "red", sorting: 6 }], keys = ['label', 'color'], filtered = listOfTags.filter( (s => o => (k => !s.has(k) && s.add(k)) (keys.map(k => o[k]).join('|')) ) (new Set) );
console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related Topics
Mutlple Owl Carousel in One Page With Different Setting
Chart.Js Draw Custom Grid Lines
How to Display JavaScript Datetime in 12 Hour Am/Pm Format
Converting Async/Await Function to Normal Es5 Targetting Ie 11
Angular:Onclick on HTML Element According to Its Class
Get High-Order Block'S Id on Click
Does Jest Swallow Console.Log Statements How to Change This
Jquery Change Event Called Two Times
Node.Js Document Is Not Defined
How to Load a Font File in Vuejs and Webpack
Json.Stringify Without Quotes on Properties
Warning: Failed Proptype: Invalid Prop 'Component' Supplied to 'Route'
After Clicking a Button of Sweet Alert Call PHP File
How to Import a Script into .Vue File
How to Merge the Property With Same Key in Two Object Array
How to Prevent Browser to Re-Set Inputs on Page Refresh