1

I have 7 arrays in javascript and I need to find values that are present in all of them.

I don't think I'm the first one to ask this but I can't find a solution for this. I read many answers but they all compare only 2 arrays and that logic don't work for multiple arrays.

I tried functions proposed in Simplest code for array intersection in javascript but they don't fit the kind of arrays I have.

The arrays I have can have different lengths in elements and the element's lengtt can vary too. I also may have zero item arrays in which they should not be compared against.

The main problem is with different number lengths. All functions I tried require sorting but this causes a problem.

Given this arrays:

xnombre = [1,2,3,4,5,24,44,124,125,165];
xacomp = [1,5,44,55,124];
xeje = [];
xanio = [1,5,44,55,124];
xini = [1,5,44,55,124];
xaporte = [1,5,44,55,122,123,124,144,155,166,245];
xpcia = [2,1,3,4,6,5,7,9,12,12,14,15,44,16,17,19,124];

The first to arrays are sorted to:

[1, 124, 125, 165, 2, 24, 3, 4, 44, 5] 
[1, 124, 44, 5, 55] 

Which when I "intersect" I only get [1,124] but 44 and 5 are missed.

Any help would be appreciated. Thanks

Community
  • 1
  • 1
Juan Ignacio
  • 3,217
  • 8
  • 33
  • 53

3 Answers3

1

The function from the other question works, but you have to sort your array numerically, not lexicographically, since you are working with numbers, not strings.

function sortNumber(a,b) {
    return a - b;
}

var xnombre = [1,2,3,4,5,24,44,124,125,165];
var xacomp = [1,5,44,55,124];

xnombre.sort(sortNumber);
xacomp.sort(sortNumber);

DEMO

To apply this to multiple arrays, you could apply this function consecutively:

// var result = intersect(a, b, c, ...);
function intersect(var_args) {
    // sort arrays here or beforehand
    var target = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        if (arguments[i].length > 0) {
            target = intersection_safe(target, arguments[i]);
        }
    }
    return target;
}
Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

This requires some of the new array methods, but it produces your desired output.

function intersection() {
    var arrs = Array.prototype.filter.call(arguments, function (a) {
        return a.length > 0;
    }).sort(function (a, b) {  // sort the arrays, so that we test the shortest.
        return a.length - b.length;
    });

    var rest = arrs.slice(1),
        test = arrs[0];

    return test.filter(function (x) { return rest.every(function (a) { return a.indexOf(x) !== -1; }); });
}

var xnombre = [1, 2, 3, 4, 5, 24, 44, 124, 125, 165], 
    xacomp  = [1, 5, 44, 55, 124], 
    xeje    = [], 
    xanio   = [1, 5, 44, 55, 124], 
    xini    = [1, 5, 44, 55, 124], 
    xaporte = [1, 5, 44, 55, 122, 123, 124, 144, 155, 166, 245], 
    xpcia   = [2, 1, 3, 4, 6, 5, 7, 9, 12, 12, 14, 15, 44, 16, 17, 19, 124];

intersection(xnombre, xacomp, xeje, xanio, xini, xaporte, xpcia)
// => [1, 5, 44, 124]
Noah Freitas
  • 17,240
  • 10
  • 50
  • 67
0

I tried your problem with underscore.

var _ = require('underscore');

xnombre = [1,2,3,4,5,24,44,124,125,165];
xacomp = [1,5,44,55,124];
xeje = [];
xanio = [1,5,44,55,124];
xini = [1,5,44,55,124];
xaporte = [1,5,44,55,122,123,124,144,155,166,245];
xpcia = [2,1,3,4,6,5,7,9,12,12,14,15,44,16,17,19,124];

var result = _.intersection(xnombre,xacomp,xanio,xini,xaporte,xpcia);

console.log(result);

But as you see that I haven't given the empty array, so somehow you have to ignore empty array.

Fiddle

Mritunjay
  • 25,338
  • 7
  • 55
  • 68