Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicates with in an object array using angular 4

I have an array with below list of items as shown in image , I would like to remove the duplicates
[L7-LO, %L7-LO] from that array.

Array with repetitions
I have tried with the following conditions:

Scenario 1 :

this.formulalist.filter((el, i, a) => i == a.indexOf(el))

Scenario 2:

Observable.merge(this.formulalist).distinct((x) => x.Value)
          .subscribe(y => {
       this.formulalist.push(y)
   });

Scenario 3:

this.formulalist.forEach((item, index) => {
        if (index !== this.formulalist.findIndex(i => i.Value == item.Value)) 
        {
            this.formulalist.splice(index, 1);
        }

    });

None of the three scenarios above were able to remove the duplicates from that array. Could any one please help on this query?

like image 531
Glory Raj Avatar asked Jan 08 '19 19:01

Glory Raj


1 Answers

angular is not necessary use vanillajs filter the elements with only one occurrence and add to the new list the first occurrence

let newFormulalist =  formulalist.filter((v,i) => formulalist.findIndex(item => item.value == v.value) === i);
like image 118
imperezivan Avatar answered Oct 02 '22 23:10

imperezivan