1

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;
  }


2
  • Is findDuplicates getting called? Also you don't need to do newArr.push(item); in both the if and else statement you can just do it after both statements since a conditional statement is synchronous. Much like whatever comes after a for loop. Commented Nov 18, 2019 at 15:48
  • @MattCroak I have updated the question according addressing your comments, thank you for letting me about the changes Commented Nov 18, 2019 at 16:49

1 Answer 1

2

Try changing findDuplicates() to this.findDuplicates()

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) => {
        if (this.attachmentForms.filter(i => i.name === item.name).length > 1) {
            item.isDuplicate = true;
        } else {
            item.isDuplicate = false;
        }
        newArr.push(item);
    });
    this.attachmentForms = newArr;
}

Here is a Stackblitz example: https://stackblitz.com/edit/angular-nl8zxc

Sign up to request clarification or add additional context in comments.

3 Comments

Not adding this while calling findDuplicates was a editing mistake from my side while posting this question. Thank you for letting me know.t I am still unable to set isDuplicate as true for name: Document1. Can you please help
Only one Document1 row has isDuplicate = true, other Document1 row has isDuplicate =false with the current code. @3limin4t0r and @shareedit gave a better solution where both Document1 row has isDuplicate = true, unfortunately they deleted the answer: Below is the code: const counter = new Map(); this.attachmentForms .forEach(({name}) => counter.set(name, (counter.get(name) || 0) + 1)); this.attachmentForms .forEach(item => (item.isDuplicate = counter.get(item.name) !== 1));
I've update the stackblitz and example code to produce the results you were looking for. I tried the code you posted, but it did not yield the results you wanted in my stack blitz. It's possible I missed a line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.