Consider the following Set
of objects spread into a new array. What gets logged?
const mySet = new Set([{ a: 1 }, { a: 1 }]);
const result = [...mySet];
console.log(result);
A) [{a: 1}, {a: 1}]
B) [{a: 1}]
Put your answer in the comments!
Consider the following Set
of objects spread into a new array. What gets logged?
const mySet = new Set([{ a: 1 }, { a: 1 }]);
const result = [...mySet];
console.log(result);
A) [{a: 1}, {a: 1}]
B) [{a: 1}]
Put your answer in the comments!
For further actions, you may consider blocking this person and/or reporting abuse
FDiaz -
Leandro Veiga -
James -
Valentin Turbins -
Top comments (4)
A, because separate objects are not deduped by a Set, even if they happen to have an identical structure and values. If you wanted it to log B, you'd have to do this:
Here the object does get deduped because it's the same exact object instance twice, not two different object instances that looks the same.
A!
This is because in JS Objects are passed by reference instead of value.
Just for fun, one of my first packages on npm was to make a superset of Set that included functionality allows you to do this by reference.
npmjs.com/package/spicy-set
B
A