Currently, I'm facing a challenge in identifying duplicated values within an array. Let's consider the scenario where we have an array of arrays:
array = [
{ id: 123, name: 'Emily', address: 'UK' },
{ id: 123, name: 'Ross', address: 'USA' },
{ id: 157, name: 'Joey', address: 'Italy' },
];
In this example, there are two arrays with the same ID id=123
. It is crucial to detect these duplicates to ensure clean data.
Specifically, my focus lies on checking for duplication based on IDs and Names simultaneously. Although I attempted a logical solution, it failed to provide accurate results by returning more rows than necessary:
ngOnInit() {
this.array.forEach((row) => {
this.array.find(element => {
if (element['id'] === row['id']) {
console.log(row)
}
})
})
}
The current output appears as follows:
123 Emily
123 Emily
123 Ross
123 Ross
157 Joey
My desired output should be:
123 Emily
123 Ross
If you want to explore further, here is a link to the code snippet on StackBlitz.