-2

I have an array with duplicates values. I need to remove duplicates out of it

this.list1 = [
               { item_id: 1, item_text: 'Maintenance' },
               { item_id: 2, item_text: 'Raw Material Plates and Profile' },
               { item_id: 3, item_text: 'Raw Material Plates and Profile' },
               { item_id: 4, item_text: 'Asset' },
               { item_id: 5, item_text: 'Asset' },
               .
               . 
               .
             ]

after removing duplicates,

this.list1 = [
               { item_id: 1, item_text: 'Maintenance' },
               { item_id: 2, item_text: 'Raw Material Plates and Profile' },
               { item_id: 3, item_text: 'Asset' },
               .
               . 
               .
             ]

And also item_id should be in order after removing duplicates. How to do this in proper way?

BLNT
  • 94
  • 2
  • 8

1 Answers1

1
var result = this.list1.reduce((unique, o) => {
    if(!unique.some(obj => obj.item_text === o.item_text)) {
      unique.push(o);
    }
    return unique;
},[]);
console.log(result); 
jobayer
  • 156
  • 1
  • 5