Compare undefined arrays for common values โ€‹โ€‹in javascript

I would like to know how to compare two or more - potentially unlimited - arrays for common values โ€‹โ€‹and use these values โ€‹โ€‹effectively in a new array. Below I have a function that will take unlimited arguments, but I'm not sure if this is a good place to start. PHP seems to have a method that can do what I want to call array_intersect. Does javascript represent something like this?

Note. I have found examples of how this can be done with two or so arrays, but I have not found examples of how such approaches can be applied to an indefinite number of arrays. Therefore, I do not see this as a duplicate question.

To clarify, arrays can be filled with anything. Letters, numbers, symbols, words, you name it, maybe there.

var sampleOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var sampleTwo = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];

function FindDirectRelation() {
    for(var i = 0; i < arguments.length; ++i) {
        console.log(arguments[i]);
        

    };
};

var directRelation = FindDirectRelation(sampleOne, sampleTwo);
Run codeHide result

I'm still new to coding, so please make sure everything is explained in a way that is easy enough to understand.

+4
source share
3 answers

using an existing intersection that works with 2 arrays, we can combine a common subset using the built-in method reduce()for an array of arrays that need to be intersected:

function intersect(a, b) {
  var aa = {};
  a.forEach(function(v) { aa[v]=1; });
  return b.filter(function(v) { return v in aa; });
}

var r1=[1,2,3], 
r2=[1,3,4,5], 
r3=[5,1,3];

alert([r1, r2, r3].reduce(intersect)) // shows: 1,3

if you define "intersecting" as just being in several arrays (not each), then it is more complex ...

+5
source

, :

function multi_intersect(a) {
  var other_arrays = Array.prototype.slice.call(arguments, 1);

  return a . filter(function(elt) {
    return other_arrays.every(function(an) {
      return an.indexOf(elt) !== -1;
    });
  });
}
0

Array.prototype.filter(), Array.prototype.indexOf()

var res = sampleOne.filter(function(val) {return sampleTwo.indexOf(val) !== -1})

var sampleOne = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
var sampleTwo = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18];
var arr = ["a", "b", "c"];
var arr1 = ["c", "d", "e"];
var arr2 = [2, 7];

function samples() {
  var args = Array.prototype.slice.call(arguments);
  var res = [];
  for (var i = 0, curr, next; i < args.length; i++) {
    if (args[i + 1]) {
      // set `curr` to array `i`
      curr = args[i];
      // set `next` to array `i + 1` if it exists
      next = args[i + 1]
    } else {
      // if at last index, set `curr` to `args` : input arrays 
      // flattened to single array , with element at `i` removed
      curr = [].concat.apply([], args.slice(0, args.length - 1));
      console.log(curr)
      // set next to current index
      next = args[i];
    };
    next = next.filter(function(val) {
      return curr.indexOf(val) !== -1 
             // filter duplicate entries at `res`
             && res.indexOf(val) === -1
    });
    res = res.concat.apply(res, next);
  };
  return res
}
var sample = samples(sampleOne, sampleTwo, arr, arr1, arr2);
console.log(sample); // [5, 6, 7, 8, 9, 10, 11, 12, "c", 2]
Hide result
-1

Source: https://habr.com/ru/post/1621059/


All Articles