Search code examples
javascriptsalesforce-lightning

issue with javascript set.has in lwc


I'm trying to use set.has() in lightning web components and it seems to be not working.

Below is the code snippet..

sStatusToVerify = 'Complete';
var setStatusVals = [...new Set(this.lstAllData.map(obj => obj.sStatus))];
console.log('setStatusVals  : ',setStatusVals);
console.log('Contains?? : ' ,setStatusVals.has(sStatusToVerify));

setStatusVals consoles all the values and it contains "Complete". However, the next console is not printed at all. It should ideally print true. Not sure why this is not working.

What is wrong here?


Solution

  • The problem with your solution is , you are converting the set back to array by using the spread operator [... new Set()] and array does not has has method . Hence the issue

    var sStatusToVerify = 'Complete';
    
    var arr=[{sStatus:'Complete'},{sStatus:'Start'},{sStatus:'InProgress'}];
    
    var setStatusVals = new Set(arr.map(obj => obj.sStatus));
    
    console.log(setStatusVals.has(sStatusToVerify));