Trying to get two data sets to intersect but I can't do it. For example, in my code below, intersecting mySet and mySet2 should yield "1" since they both have a value of "1" in their set.
var mySet = new Set();
var mySet2=new Set();
mySet.add(1);
mySet.add(2);
mySet.add("HELLOOOOO");
mySet2.add("hi");
mySet2.add(1);
var a = Array(mySet, mySet2);
console.log(a);
mySet.forEach(function(value) {
console.log(value);
});
mySet2.forEach(function(value) {
console.log(value);
});
function intersection_destructive(a, b)
{
var result = new Array();
while( mySet.length > 0 && mySet2.length > 0 )
{
if (mySet[0] < mySet2[0] ){ mySet.shift(); }
else if (mySet[0] > mySet2[0] ){ mySet2.shift(); }
else /* they're equal */
{
result.push(mySet.shift());
mySet2.shift();
}
}
return result;
}
Set 1 and Set 2 both have "1" in it but my function (intersection_destructive) doesn't return it. I'm not sure how to intersect them, I searched stackoverflow and found intersection_destructive but it didn't work for me, I also tried:
array1.filter(function(n) {
return array2.indexOf(n) != -1
});
as per this: Simplest code for array intersection in javascript
but I get an error on filter
when I try to run it.