I have a requirment where i need to check for only duplicate description in array of objects. Below is the object example.
traveler = [
{ description: 'Senior', Amount: 50},
{ description: 'Senior', Amount: 10},
{ description: 'Adult', Amount: 75},
{ description: 'Child', Amount: 35},
{ description: 'Infant', Amount: 25 },
{ description: 'Adult', Amount: 105},
];
In the above array traveler there are objects with duplicate description, so how to check only for duplicate description throught out the array and display a message.
I am using reduce method,below is the code. but the control is not going inside the if loop
Am i going wrong somewhere?
var res = traveler.reduce((acc, obj)=>{
var existItem = acc.find(item => item.description === obj.description);
if(existItem){
return this.toaster.pop('info', 'Insertion unsuccessful', 'Duplicate answer found')
}
});