How to calculate intersection of multiple arrays in JavaScript? And what does [equals: function] mean?

前端 未结 12 2129
广开言路
广开言路 2020-11-29 06:13

I am aware of this question, simplest code for array intersection but all the solutions presume the number of arrays is two, which cannot be certain in my case.

I ha

相关标签:
12条回答
  • 2020-11-29 06:27

    Small recursive divide and conquer solution that does not rely on es6 or any library.

    It accepts an array of arrays which makes the code shorter and allows you to pass arguments by using map.

    function intersection(a) {
        if (a.length > 2)
            return intersection([intersection(a.slice(0, a.length / 2)), intersection(a.slice(a.length / 2))]);
    
        if (a.length == 1)
            return a[0];
    
        return a[0].filter(function(item) {
            return a[1].indexOf(item) !== -1;
        });
    }
    
    var list1 = [ 'a', 'b', 'c' ];
    var list2 = [ 'd', 'b', 'e' ];
    var list3 = [ 'f', 'b', 'e' ];
    console.log(intersection([list1, list2, list3]));

    0 讨论(0)
  • 2020-11-29 06:27

    Arg0n's answer is great, but just in case anyone is looking for intersection of object arrays, i modified Arg0n's answer a bit to deal with it.

    function getIntersectedData() {
        var lists = [[{a:1,b:2},{a:2,b:2}],[{a:1,b:2},{a:3,b:3}],[{a:1,b:2},{a:4,b:4}]];
        var result = [];
        for (var i = 0; i < lists.length; i++) {
            var currentList = lists[i];
            for (var y = 0; y < currentList.length; y++) {
                var currentValue = currentList[y];
                if (customIndexOf(result,currentValue)) {
                    var existsInAll = true;
                    for (var x = 0; x < lists.length; x++) {
                        if(customIndexOf(lists[x],currentValue)){
                            existsInAll = false;
                            break;
                        }
                    }
                    if (existsInAll) {
                        result.push(currentValue);
                    }
                }
            }
            return result;
        }
    }
    
    function customIndexOf(array,value){
        var notFind = true;
        array.forEach(function(element){
            if(element.a === value.a){
                notFind = false;
            }
        });
        return notFind;
    }
    
    0 讨论(0)
  • 2020-11-29 06:37

    You could just use Array#reduce with Array#filter and Array#includes.

    var array1 = ["Lorem", "ipsum", "dolor"],
        array2 = ["Lorem", "ipsum", "quick", "brown", "foo"],
        array3 = ["Jumps", "Over", "Lazy", "Lorem"],
        array4 = [1337, 420, 666, "Lorem"],
        data = [array1, array2, array3, array4],
        result = data.reduce((a, b) => a.filter(c => b.includes(c)));
    
    console.log(result);

    0 讨论(0)
  • 2020-11-29 06:37

    I manage to accomplish this with a reduce call:

    var intersected = intersect([[1, 2, 3], [2, 3, 4], [3, 4, 5]]);
    console.log(intersected); // [3]
    
    function intersect(arrays) {
        if (0 === arrays.length) {
            return [];
        }
        return arrays.reduce((intersection, array) => {
            return intersection.filter(intersectedItem => array.some(item => intersectedItem === item));
        }, arrays[0]);
    }

    0 讨论(0)
  • 2020-11-29 06:40

    Your code with _lodash is working fine.

    As you can say in this fiddle:

    this code:

    var arrayOfArrays = [[4234, 2323, 43], [1323, 43, 1313], [23, 34, 43]];
    var a = _.intersection.apply(_, arrayOfArrays);
    console.log(a);
    console.log(a.length);
    

    Will have output:

    [42]
    1
    

    Maybe you see

    equals: function

    because you are using kind of debugger.

    Try to just print the array with console.log, you will get only 42.

    0 讨论(0)
  • 2020-11-29 06:44

    Lodash pure:

    _.keys(_.pickBy(_.groupBy(_.flatten(arrays)), function (e) {return e.length > 1}))
    

    Lodash with plain js:

    var elements = {}, duplicates = {};
     _.each(arrays, function (array) {
         _.each(array, function (element) {
             if (!elements[element]) {
                 elements[element] = true;
             } else {
                 duplicates[element] = true;
             }
         });
     });
    _.keys(duplicates);
    
    0 讨论(0)
提交回复
热议问题