2

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')
            } 
           });
2

5 Answers 5

5

Try this.

reduce(){
   let res=[];
   this.traveler.map(function(item){
     var existItem = res.find(x=>x.description==item.description);
     if(existItem)
      console.log("item already exist");
     else
      res.push(item);
   });
   console.log(res);
 }
Sign up to request clarification or add additional context in comments.

Comments

3

remove reduce and use only filter.

let existItem = traveler.filter(item => item.description === obj.description);
if (existItem.length > 1) {
    return this.toaster.pop('info', 'Insertion unsuccessful', 'Duplicate answer found')
}

1 Comment

where does 'obj' came from ?
1

You can write the code:

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

function hasDuplicateDesc(collection) {
  var groups = collection.reduce((acc, cur) => {
    acc[cur.description] = (acc[cur.description] || 0) + 1;
    return acc;
  }, {});

  return Object.values(groups).some(num => num > 1);
}

if (hasDuplicateDesc(traveler)) {
  return this.toaster.pop(
    "info",
    "Insertion unsuccessful",
    "Duplicate answer found"
  );
}

groups is an object which basically creates an object:

{
  "Senior": 2,
  "Adult": 2,
  "Child": 1,
  "Infant": 1
}

We grab the values the object using Object.values(groups), and check if there is a single number which is grater than 1. If exists any num > 1, then we can say duplicate description is present.

1 Comment

Thanks for the solution but i am getting a error, Property 'values' does not exist on type 'ObjectConstructor'.
1

The control is not going inside the if block because you're not pushing anything into acc (and you didn't initialized it in the last argument of reduce), the reduce operation won't do it for you.

This solution should work:

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'
        );
    }
    acc.push(obj);
    return acc;
}, []);

With this res will contain unique values and you will be notified of duplicates with your toaster.pop. But you can also keep the duplicates by pushing into a duplicateList inside your if block.

Comments

0

here iam trying to figure it out duplicate emails in the array. i have enteredEmails array which is having the list of emails which may unique or duplicate.

please find the below example.


this.enteredEmails.forEach(emailItr => {

      matchedEmail = [];
      matchedEmail = this.enteredEmails.filter((val) => val.emailId === emailItr.emailId);

      if (matchedEmail.length > 1) {
        return this.isDuplicate = true;

      }

    });

}

Comments

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.