Skip to content

Сreating an array of unique values in JavaScript using Sets with empty arrays in the array #187

Discussion options

You must be logged in to vote

How so?

Not saying this is the definition, but if we take this one:

Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection

Then it isn't violating the concept of Set since the values are unique, you are passing in javascript references to objects by design and [] an object. Those things are different no matter how you look at them, so the set should contain both.

What would happen if it wasn't true we wrote instead?

const a = [];
const b = [];
const c = new Set([a, b]);
a.push(2);
b.push(1);
// c = [ [1] ]; or c = [ [2] ]; Which would expect? or do you expect c …

Replies: 6 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by denysdovhan
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #118 on February 05, 2021 14:53.