Salesforce Fact #819 | Checking duplicates in array in LWC
Many times in LWC or JS, we need to check if an array of primitive types contains a duplicate or not.
The indexOf() method can be used to identify the same.
Example:
const arr = ['a', 'b', 'c', 'a'];
const result = arr.filter((row, index)=>arr.indexOf(row)!=index)
if(result.length > 0){
console.log('array contains duplicate');
}
else{
console.log('array does not contain duplicate');
}
Reference: https://flexiple.com/javascript/find-duplicates-javascript-array
Comments
Post a Comment