Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript intersection recursion

const array1 = ["Lorem", "ipsum", "dolor"];
const array2 = ["Lorem", "ipsum", "quick", "brown", "foo"];
const array3 = ["Jumps", "Over", "Lazy", "Lorem"];
const array4 = [1337, 420, 666, "Lorem"];

const arrayOfArrays = [[4234, 2323, 43], [1323, 43, 1313], [23, 34, 43]];

// Filter xs where, for a given x, there exists some y in ys where y === x.
const intersect2 = (xs,ys) => xs.filter(x => ys.some(y => y === x));

// When there is only one array left, return it (the termination condition
// of the recursion). Otherwise first find the intersection of the first
// two arrays (intersect2), then repeat the whole process for that result
// combined with the remaining arrays (intersect). Thus the number of arrays
// passed as arguments to intersect is reduced by one each time, until
// there is only one array remaining.
const intersect = (xs,ys,...rest) => ys === undefined ? xs : intersect(intersect2(xs,ys),...rest);

console.log(intersect(array1, array2, array3, array4));
console.log(intersect(...arrayOfArrays));

// Alternatively, in old money,

var intersect2ES5 = function (xs, ys) {
    return xs.filter(function (x) {
        return ys.some(function (y) {
            return y === x;
        });
    });
};
    
// Changed slightly from above, to take a single array of arrays,
// which matches the underscore.js approach in the Q., and is better anyhow.
var intersectES5 = function (zss) {
    var xs = zss[0];
    var ys = zss[1];
    var rest = zss.slice(2);
    if (ys === undefined) {
        return xs;
    }
    return intersectES5([intersect2ES5(xs, ys)].concat(rest));
};

console.log(intersectES5([array1, array2, array3, array4]));
console.log(intersectES5(arrayOfArrays));
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: upload image to server react next 
Javascript :: npm init step by step 
Javascript :: nodejs post req accept form data 
Javascript :: p5 js stop video camera capture 
Javascript :: typeorm caching queries time limit by id 
Javascript :: react pdf fixed property not working 
Javascript :: dynamic data fetching in datatable.net 
Javascript :: new http version ANGULAR 
Javascript :: javascript string to date format dd/mm/yyyy 
Javascript :: code to sum of specific nodes in binary tree for int kDistancefrom node(struct Tree,int k,int n); 
Javascript :: regex match caret 
Javascript :: jsondb 
Javascript :: Custom usePagination hook 
Javascript :: Spread syntax in ES6 
Javascript :: architecture express.js 
Javascript :: stimulus controller 
Javascript :: visable in viewport 
Javascript :: replace div content javascript 
Javascript :: for loop display numbers 1 to 10 javascript 
Javascript :: programmatically change mongoose schema enum values 
Javascript :: change candle color react highcharts 
Javascript :: enquire js - simple media query library for Javascript 
Javascript :: how to bind multiple value in javascript 
Javascript :: initialize back4app 
Javascript :: xmlhttprequest set route params 
Javascript :: how to call AWS Serverless api in Node/JS 
Javascript :: changing CSS with JS, using a function - strips all CSS and re-adds classes passed by 2nd parameter - as an Array 
Javascript :: cypher neo4j 
Javascript :: form react js 
Javascript :: const { message } 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =