4

I am trying to collect record Ids of changed records. Therefor I'm using aura:attribute type="Set". But whenever I add the same id twice, it will be twice in my set. As described in the documentation, there should be no duplicates. (Current Worakround is using type="Map").

<aura:attribute name="mySet" type="Set" default="[]" access="private" />

Controller:

var recordId = "a0246000005mfR0AAI";
var mySet = cmp.get("v.mySet");

mySet.push(recordId);
mySet.push(recordId);
cmp.set("v.mySet", mySet);

var doublecheck = cmp.get("v.mySet");

Debug result:

doublecheck = Proxy {0: "a0246000005mfR0AAI", 1: "a0246000005mfR0AAI", length: 2}

Am I handling the set wrong, or does it just not work yet?

  • Have you tried using add instead of push? – Raul Dec 13 '17 at 11:44
  • @RahulSharma Yep, it told me mySet.add is not a function – Basti Dec 13 '17 at 11:58
8

I think that the problem is in javascript when you are declaring mySet.

Try this:

var mySet = new Set(component.get("v.mySet"));

Edit: the best solution is

var changes = cmp.get("v.changes") || new Set();
  • 2
    Worked, thanks! But if this is the solution, type="Set" seems to be useless, if I have to take care myself. Btw. I was able to do it like this: var changes = cmp.get("v.changes") || new Set(); so the default [] seems not to do the right thing :/ – Basti Dec 13 '17 at 11:55
  • 2
    [] is an array initializer, I wouldn't expect it to be equal to new Set() – battery.cord Dec 13 '17 at 14:43
  • 1
    It seems to be weird. I also believe type="Set" should have taken care of duplicates itself. Any thoughts? – Arpit Sethi Dec 13 '17 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.