[Vuejs]-Find duplicate values Vuejs

1👍

You can check before you add

  const newItem; // comes from request

  const duplicateIndex = this.items
    .map(item => item.name)
    .indexOf(newItem.name);
  // returns index of Object that has same name as newItem's name 
  // or -1 if not found

  // Post request
  if (newItem.id == null) {
    if (duplicateIndex) {
      this.$notify({
        group: "notify",
        text: "Duplicate value! ",
        type: "error"
      });
    } else {
      this.items.push(newItem);
    }
  // Put request
  } else {
    this.items[duplicateIndex] = newItem;
  }

0👍

This might help, Idea is to get the existing Id of record, and match with other records, if other record’s id and this id mis-match then you can say this is proper duplicate value

this.duplicate = false;

for(let item of this.items){ 

var existingId=this.item.id || -1; // null coalescing operator (??) is using a logical OR (||):


        
        if (existingId!=item.id && this.item.name.toUpperCase().indexOf(item.name.toUpperCase()) == -1) {
           this.$notify({
                group: "notify",
                text: "Duplicate value! ",
                type: "error"
            });
            this.duplicate = true ;
        } else {
             //nothing found
        }        
    }

    if(!this.duplicate){
        // post/put request
    }

Leave a comment