1

I have a custom label Restriced_Profiles, which contains a comma-separated string of Profile Names - Profile A, Profile B , System Administrator

In the aura controller, I am trying to convert this string to a set so that I can check if the logged-in user profile contains in the set.

Problem: I tried using split to convert the string to set but when I print it in the console it still shows as a string. So unable to do a .has test on this. Below is the code:

    let prfiles = $A.get("$Label.c.Restriced_Profiles");
    let setOfProfiles = prfiles.split(",");
    console.log('setOfProfiles ------->' + setOfProfiles); // It still shows `Profile A, Profile B , System Administrator`

It appears that split is not converting it from string to set. Am I missing anything here?

1 Answer 1

1

You're using +, and a string + an object calls toString() on that object, which for an Array, causes the elements to be converted into a comma separated string of values:

let output = ''+['a','b','c'];
console.log(output); //'a,b,c'

Instead, use the comma operator:

console.log('setOfProfiles ------->', setOfProfiles);

Which will output an object you can inspect with all the values.

Note that you still don't have a Set, you have an Array. You can:

let setOfProfiles = new Set($A.get("$Label.c.Restricted_Profiles").split(","));

The moral of the story is that you should be careful when using + as a string concatenation, both in JavaScript and Apex, because you may get "unexpected" output in the debug logs.

1
  • Thank you for an insightful answer.
    – SfdcBat
    Feb 9, 2022 at 17:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .