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

Popular posts from this blog

Salesforce Fact #192 | Call batch apex from flow

Salesforce Fact #457 | Get current user role name in flow