I am trying to find all the duplicates in the attachmentForms list in Angualar 8. I searched through a few posts and below is the code that I could come up with but I am still unable to set isDuplicate as true for name: Document1. I appreciate any help on this.
export interface AttachmentForm {
name: string;
isDuplicate: boolean;
}
ngOnInit() {
this.attachmentForms = [
{name: 'Document1', isDuplicate: false},
{name: 'Document2', isDuplicate: false},
{name: 'Document1', isDuplicate: false},
{name: 'Document3', isDuplicate: false},
];
this.findDuplicates() ;
}
findDuplicates() {
const newArr: AttachmentForm[] = [];
this.attachmentForms.forEach((item, index) => {
if (newArr.findIndex(i => i.name === item.name) === -1) {
item.isDuplicate = false;
} else {
item.isDuplicate = true;
}
newArr.push(item);
});
this.attachmentForms = newArr;
}
findDuplicatesgetting called? Also you don't need to donewArr.push(item);in both theifandelsestatement you can just do it after both statements since a conditional statement is synchronous. Much like whatever comes after aforloop.