Home > Blockchain >  How can I find duplicate records from array in JS
How can I find duplicate records from array in JS

Time:10-08

I am trying to find duplicate records from a given array. I have tried the following function, but it did not work as expected, later I come to know I was comparing the same record with itself. I have written the following function.

identifyDupliateRecords(verifiedRecordsAll) {
    let duplicateRecords : any;
    if(verifiedRecordsAll.length > 1){
      const tempDuplicateRecords = []
      this.verifiedRecordsAll.forEach((record)=> {
        if(verifiedRecordsAll.Some === verifiedRecordsAll.Some ||
          verifiedRecordsAll.Gum === verifiedRecordsAll.Gum ||
          verifiedRecordsAll.Thing === verifiedRecordsAll.Thing) {
          tempDuplicateRecords.push(record);
          duplicateRecords = tempDuplicateRecords
        }
      })
    }
    return duplicateRecords
  }

For finding duplicate records I need to have such a condition that if Some or Gum or Thing property of one record should match the value of another record in the same array, then it should be stored as a duplicate. I am new to JS and cannot think of any other solution. Any help would be appreciated. Thank you.

CodePudding user response:

this.verifiedRecordsAll=[] --Array need to add

this.verifiedRecordsAll =this.verifiedRecordsAll.filter((element, i) => i === this.verifiedRecordsAll.indexOf(element))

CodePudding user response:

const yourArray=[] let duplicatedRecords=[]

for(let k=0;k<array.length;k  ){
   for(let i=0;i<array.length;i  ){
     if(k!==i&&array[k]===array[i]){
        duplicatedRecords.push(array[k])
     }
  } 
}
duplicatedRecords= [...new Set(duplicatedRecords)]
  • Related