I have an array like this
arrays = [
['a', 'b', 'c', 'd'],
['a', 'b', 'c', 'g'],
['a', 'b', 'c', 'g', 'x'],
]
I need to form one array of all intersection. Like this.
function get_intersects(arrays) {
// return all intersections
return ['a', 'b', 'c', 'g'];
}
notice how g
is returned even though it is not in all three but it is in at least 2 of them.
This is my attempt but the g
is missing.
arrays = [
['a', 'b', 'c', 'd'],
['a', 'b', 'c', 'g'],
['a', 'b', 'c', 'g', 'x'],
]
function get_intersects(arrays) {
return arrays.shift().filter(function (v) {
return arrays.every((a) => a.indexOf(v) !== -1 );
});
}
console.log(get_intersects(arrays))