function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajath Kundapur 17Rajath Kundapur 17 

In LWC, How to check duplicate value in array

I have 2 array

-> The First array consists of all the values.

-> In the second array, consist of selected values  Here I need to filter the value which is already present

Best Answer chosen by Rajath Kundapur 17
Danish HodaDanish Hoda
Hi Rajath,
secondArray.forEach(elem => {
   if(firstArray.includes(elem)){
      const index = firstArray.indexOf(elem);
      firstArray.splice(index, 1);
   }
});

 

All Answers

Danish HodaDanish Hoda
Hi Rajath,
Don't know the correct answer, but I would use a forEach() method and check if the first array contains thiselement as below:

secondArray.forEach(elem => {
if(firstArray.includes(elem)){
//my logic
}
});
Rajath Kundapur 17Rajath Kundapur 17

Thanks Danish

The solution is working. Could you please suggest me how to remove duplicate elements from the second array

Danish HodaDanish Hoda
Hi Rajath,
secondArray.forEach(elem => {
   if(firstArray.includes(elem)){
      const index = firstArray.indexOf(elem);
      firstArray.splice(index, 1);
   }
});

 
This was selected as the best answer
Rajath Kundapur 17Rajath Kundapur 17
Now my function is working properly. Thanks, Danish for the suggestion.