Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
duplicate check in array when edit the data in array

I push the data into an array. Before pushing, I check name already exists or not.

  1. I checked whether the same name available in the array or not

  2. If it exists alert throws " duplicate items"

  3. If it is a new item, it will push into an array.

The below code has been working fine till now

But when I edit the existing item address field, it will throw the duplicate alert items

this.arrayData = [{
    "name": "sachin",
    "address": "india"
}, {
    "name": "bin",
    "address": "us"
}, {
    "name": "gill",
    "address": "aus"
}]

var isInArray = this.arrayData.filter(c=>c.name === this.data.name);

if (isInArray.length > 0) {
    alert("dublicate items");
} else {
    this.saveItems(this.data);
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you dont want duplicates on array, just use Set, example code:


let abc = ['a', 'b', 'c', 'd']

function saveItems(newItem) {
  abc = [...abc, newItem]
  abc = [...new Set(abc)]
}

saveItems('d')
saveItems('d')
saveItems('e')

console.log(abc)

//output: (5) ["a", "b", "c", "d", "e"]

about 4 years ago · Juan Pablo Isaza Report

0

I tried below solution to achieve the result.

 for(var i=0;i < this.arrayData.length;i++)
                 {       
                    if(i=== index) 
                    { 
                        if(this.arrayData[i].Name === this._data.Name )
                        { 
                            this.saveItems(this.data);  break;
                        }
                         
                        else if(this.arrayData[i].Name !== this._data.Name )
                        { 
                            var dataCheck = this.editDublicate(index);
                            if(!dataCheck) {
                                this.saveItems(this.data);   break;
                            } 
                        }
                    }
                }  

    editDublicate(index)
{
    var dublicateData =false;
     for(var i=0;i < this.arrayData.length;i++)
     {
        if(this.arrayData[i].Name === this._data.Name &&  i !== index)
        { 
             alert("dublicate items");
            break;
        } 
     }
    return dublicateData;        
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!