Posts

Showing posts with the label array

Salesforce Fact #827 | Adding object in middle in LWC array of objects

Image
In LWC, Many times we deal with the scenario of adding an object conditionally in JS array of objects. And it needs to be added in the middle based on some condition. In this case the spread operator and the ternary operator combo works together. Reference:  https://stackoverflow.com/questions/70385371/js-conditionally-adding-adding-in-the-middle-of-an-array Attached is an example screenshot.

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