How to remove duplicate object from an array in angular 6
Asked Answered
S

6

13

I am trying to remove duplicate value objects in an array but not working... I think duplicate function is working but not reflecting in li list. Can you find out where I have to change?

My service file:

 addComp(Names,c){   
 this.item.push({ name: Names, componentid: c});
 this.uniqueArray = this.removeDuplicates(this.item, "name"); //this line issue
 this.item=this.uniqueArray; //this line issue
 }
Seniority answered 5/12, 2018 at 17:10 Comment(0)
S
7

If addComp is the only place you modify this.item, just check for existing prior to insertion. Duplicates will never get put in the array, so you'll never have to trim them.

addComp(Names,c){
  let item = {name: Names, componentid: c};
  if (this.item.find((test) => test.name === Names) === undefined) {
    this.item.push(item);
  }
}

Alternatively, if there are other places that you're modifying this.item, you should be stripping duplicates in a more expected place. Stripping them as a side-effect of the addComp function is unexpected. However, you could do it...

addComp(Names,c){
  this.item.push({name: Names, componentid: c});
  this.item = this.item.filter((test, index, array) =>
     index === array.findIndex((findTest) =>
        findTest.name === test.name
     )
  );
}
Sherer answered 5/12, 2018 at 17:19 Comment(2)
Thanks..Working fineSeniority
I have some question in angular 6..Can you answer me?Seniority
T
21
const result = Array.from(this.item.reduce((m, t) => m.set(t.name, t), new Map()).values());

This might be fix your issue.

Tanked answered 7/12, 2018 at 9:10 Comment(0)
B
12
this.item = this.item.filter((el, i, a) => i === a.indexOf(el))
Bromberg answered 24/2, 2019 at 19:19 Comment(0)
S
7

If addComp is the only place you modify this.item, just check for existing prior to insertion. Duplicates will never get put in the array, so you'll never have to trim them.

addComp(Names,c){
  let item = {name: Names, componentid: c};
  if (this.item.find((test) => test.name === Names) === undefined) {
    this.item.push(item);
  }
}

Alternatively, if there are other places that you're modifying this.item, you should be stripping duplicates in a more expected place. Stripping them as a side-effect of the addComp function is unexpected. However, you could do it...

addComp(Names,c){
  this.item.push({name: Names, componentid: c});
  this.item = this.item.filter((test, index, array) =>
     index === array.findIndex((findTest) =>
        findTest.name === test.name
     )
  );
}
Sherer answered 5/12, 2018 at 17:19 Comment(2)
Thanks..Working fineSeniority
I have some question in angular 6..Can you answer me?Seniority
A
2

This will remove existing duplicates in this.item

const item = [...new Set(this.item)];

This is a more updated way to do this. this will check for existing prior to insertion. And if item is not in this.item then this.item.indexOf(item) = -1

This is the best way to prevent pushing duplicate value objects in to an array

addComp(Names,c){
  let item = {name: Names, componentid: c};
  if (this.item.indexOf(item) === -1) {
    this.item.push(item);
  }
}
Apart answered 16/12, 2020 at 5:14 Comment(0)
A
0

This will fix the bug

const uniqueObjectArray = [...new Map(arrayOfObjectsWithDuplicates.map(item => [item[key], item])).values()]
Abbe answered 21/8, 2020 at 11:28 Comment(0)
S
0

For instance, you have an array:

var ar = [ 
{key: 1, value: 'aa'},
{key: 2, value: 'bb'},
{key: 1, value: 'aa'},
]

To filter it and eliminate duplicates based on a key, use the following:

ar.filter((el, i, a) => i === a.indexOf(a.find(f => f.key === el.key)))
Saloma answered 23/1 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.