Encountering an issue while working on Angular 7: unable to return distinct or unique objects based on LocId.
The goal is to retrieve unique objects from an array of objects containing all Locations.
allLocations:any[]=[];
ngOnInit()
{
this.locationsService.allLocations.forEach(loc => {
let d = this.locationsService.allLocations.findIndex(x => x.Locid == loc.Locid);
if (d !== -1) {
this.locationsService.allLocations.splice(d, 1);
}
The HTML component code is as follows:
<tr *ngFor="let l of locationsService.allLocations">
<td class="z2-boxstyle1-td-colcontent">
<div> {{l.Locid}} </div>
</td>
</tr>
The current result of the array is:
[
{"Locid":40903,"GPS1":"42.5913974,-71.3277873","CompanyID":1000339},
{"Locid":40900,"GPS1":"42.588432,-71.31720900000001","CompanyID":1000339}
{"Locid":40900,"GPS1":"42.588432,-71.31720900000001","CompanyID":1000339}
]
Since the LocId is the same for two objects, I need to return only one distinct object as below:
Expected Result :
[
{"Locid":40903,"GPS1":"42.5913974,-71.3277873","CompanyID":1000339},
{"Locid":40900,"GPS1":"42.588432,-71.31720900000001","CompanyID":1000339}
]