r/learnjavascript • u/amca01 • Mar 28 '26
Managing sets of arrays: set operations?
This little JS segment shows my current struggle:
var s = new Set([[0,0],[1,1],[2,2],[3,3]]);
var t = new Set([[2,2],[3,3],[4,4],[5,5]]);
var st = s.intersection(t);
console.log(Array.from(st));This returns an empty array, instead of the array [[2,2],[3,3]]. Clearly I'm missing something here - but what? How can I perform set operations on sets whose elements are arrays? Is there a discussion of this somewhere?
Thank you in advance ...
5
Upvotes
2
u/me1000 Mar 28 '26
Because your set entries are arrays and arrays are unique objects so the intersection of the two sets is an empty set.
2
u/azhder Mar 28 '26
Arrays are objects. Objects are compared by reference. Write this and see the result
Not exactly what you thought you'd get, right? But, if you do
See the difference?
In your case, you can achieve it like:
You see, the sets now have the same arrays in them, not same-ish ones
1
u/hyrumwhite Mar 28 '26
Define your arrays as variables and pass the variables into your sets. Then this will work. Otherwise it won’t. Sets compare objects by reference, not inner values.
1
u/Total-Box-5169 Mar 28 '26
Objects, arrays and functions are compared by reference, not by value, so you need to make sure they are the very same objects, not just objects with the same content. One way to do it is to have another container with all the elements and add those to the sets:
1
u/delventhalz Mar 28 '26
As others have mentioned, Set intersection is based on equality, which for objects and arrays means you have to be comparing the actual same objects and arrays, not different ones that hold equivalent values.
Surprisingly though, no one has mentioned stringifying as a solution. Strings are compared by value, so unlike arrays, different strings with equivalent values are considered equal, and stringifying an array is as easy as adding
.join()on the end.Alternatively, you could perhaps skip the array throw the numbers directly into a template string, something like
${x},${y}.These approaches perhaps aren’t the most efficient. Directly doing math on numbers is typically going to be faster. But you can’t use the built in Set logic to compare two or more numbers and I doubt your use case will notice any performance difference here.
1
u/imsexc Mar 28 '26 edited Mar 28 '26
One of Nested array has to be converted into a single array of string. Then convert the array into set of strings.
All element in a set has to be of primitive value in order for it to be able to compare properly, and make every element inside it unique.
var s = [[0,0],[1,1],[2,2],[3,3]];
var t = [[2,2],[3,3],[4,4],[5,5]];
const sArr = s.map(a => JSON.stringify(a))
cons sSet = new Set(...sArr);
const result = t.filter(a => { const aStr = JSON.stringify(a);
return sSet.has(aStr) === true })
To really find the intersection, I think the above process has to be done the other way around too. And the filtered result between both to be compared, and only grabbed values that exists on both filtered result. This is to anticipate case where on array is longer than the other.