Answers for "javascript array union intersection"

0

javascript get intersection of two arrays

function getArraysIntersection(a1,a2){
    return  a1.filter(function(n) { return a2.indexOf(n) !== -1;});
}
var colors1 = ["red","blue","green"];
var colors2 = ["red","yellow","blue"];
var intersectingColors=getArraysIntersection(colors1, colors2); //["red", "blue"]
Posted by: Guest on August-01-2019
0

js set operations union intersection

// Union (a ∪ b)
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let union = new Set([...a, ...b]);
    // {1,2,3,4}

// Intersection (a ∩ b)
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let intersection = new Set(
    [...a].filter(x => b.has(x)));
    // {2,3}

// Difference (a \ b)
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let difference = new Set(
    [...a].filter(x => !b.has(x)));
    // {1}
Posted by: Guest on June-02-2021

Code answers related to "javascript array union intersection"

Code answers related to "Javascript"

Browse Popular Code Answers by Language