Within my component, I have set up a subscription to a subject
that is returning employee records and storing them in an array.
importResults: ImportResults[] = [];
constructor(
private _massEmpService: MassEmpService
) {
}
ngOnInit() {
// Subscribing to the subject for added employees data
this._massEmpService.importedData.subscribe(obj => {
if (obj) {
// Keeping imported data results unique
this.importResults.push(_.uniqBy(obj, 'QID'));
}
});
}
The problem I am facing is that every time the button is clicked and new data is received through this subscription, it appends the new data to the existing array instead of adding only the new records that are not already present.
Here's an example with a single record received (entering one user name and clicking search)
https://i.sstatic.net/hK8bX.png
And here's an example where I add one more user while keeping the previous one in the search field.
https://i.sstatic.net/8Jhef.png
As seen, the first array represents the original search result. The second array contains both the initial employee and the new one.
My desired outcome is to have a single array with unique objects. In this case, there should be two records since the first employee was searched twice, meaning they shouldn't be duplicated in the array as the object already exists.
Could it be that I am using the lodash
function incorrectly? Also, note that QID
serves as a unique identifier within the object (not shown).