-1

I have a map attributes: Map<number, Set<string>> = new Map(); I am trying to add new values to individual sets by using the number as key like

this.attributes.get(this.selectedId).add($event.id);

This used to work but for some reasons now when i do this the value is added in all sets. I have no idea what is going on, cause to me make no sense. Does anybody had a similar problem, please help.

  • 3
    We need more code than this. But what's probably happening is that all your "sets" are actually the same instance, which is why you see an added value in all of them. – Vojtěch Strnad Oct 01 '21 at 17:17
  • I think more code isn't relevant, the point here is that i am using a key to get the right set and add the value to it. Furthermore this used to work like 2 days ago, (for four month already). – userr not found Oct 01 '21 at 17:20
  • I think you should show more code. We cannot even see the other sets. It is difficult to tell what might be happening – Tushar Shahi Oct 01 '21 at 17:22
  • There is no more in fact. I have a map with sets, and I am trying to add a value to a specif sets. Meaning if I tried this.attributes.get(2).add(hello); I don't want to add this value for the rest of the sets in the map. – userr not found Oct 01 '21 at 17:26
  • "I have a map with sets", that's the thing, you do not show how those sets are created and that is very likely the source of the error and this also probably has nothing to do with TypeScript. – H.B. Oct 01 '21 at 17:30
  • Ok, i understand, i am cloning the previous set, and in the end, I am adding it to the map //this.attributes.set(this.selectedId, origAttributes); does this helps? – userr not found Oct 01 '21 at 17:37
  • 1
    That is probably *not* a clone but just the same instance. `var x = y` is only a "clone" if `y` is a [primitive like a number](https://developer.mozilla.org/en-US/docs/Glossary/Primitive). – H.B. Oct 01 '21 at 17:44
  • gimme a min please to fix this – userr not found Oct 01 '21 at 17:46
  • Essentially just a duplicate of: [Why does changing an Array in JavaScript affect copies of the array?](https://stackoverflow.com/questions/6612385/why-does-changing-an-array-in-javascript-affect-copies-of-the-array) – H.B. Oct 01 '21 at 17:47
  • Thank you @H.B. The problem was the clone. – userr not found Oct 01 '21 at 17:51
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 08 '21 at 16:22

1 Answers1

1
this.attributes.set(this.selectedId, origAttributes);

This does not clone; the object origAttributes is stored by reference in the map. If you were to reuse or change origAttributes you would see that take effect in the same map.

let someSet = new Set([1, 2, 3]);
let someMap = new Map();

someMap.set("abc", someSet);
someMap.set("def", someSet);

console.log(`abc = ${Array.from(someMap.get("abc"))}`); // 1, 2, 3
console.log(`def = ${Array.from(someMap.get("def"))}`); // 1, 2, 3

someMap.get("abc").add(4);

console.log(`abc = ${Array.from(someMap.get("abc"))}`); // 1, 2, 3, 4
console.log(`def = ${Array.from(someMap.get("def"))}`); // 1, 2, 3, 4

Luckily, it is easy enough to clone a set, since new Set(iterable) can be passed a Set as well.

let someSet = new Set([1, 2, 3]);
let someMap = new Map();

someMap.set("abc", new Set(someSet)); // new instance
someMap.set("def", new Set(someSet)); // new instance

console.log(`abc = ${Array.from(someMap.get("abc"))}`); // 1, 2, 3
console.log(`def = ${Array.from(someMap.get("def"))}`); // 1, 2, 3

someMap.get("abc").add(4);

console.log(`abc = ${Array.from(someMap.get("abc"))}`); // 1, 2, 3, 4
console.log(`def = ${Array.from(someMap.get("def"))}`); // 1, 2, 3
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251