JavaScript that takes a multidimensional array and one array and finds matches of one array in a multi-d array

I tried to address a similar issue in a stack overflow, but it did not satisfy exactly what I was trying to do, and I continue to try to get it to work, but constantly get the wrong results. At this moment, I'm just at a loss. Example of what i mean

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]]
var compareArray = [9,11,13,15,1,2,5,6]

function intersect(a, b) {
//code to compare both arrays and find a match
}

console.log(Intersect(compareArray, masterArray)) 

And the output will be

[9,11,13,15]
[1,2,5,6]
[5,13]
[13,15]
+4
source share
5 answers

Use Array.prototype.reduce to get an array of all intersections like this:

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]];
var compareArray = [9,11,13,15,1,2,5,6];


function intersect(multi, simple) {
  return multi.reduce(function(res, b) {
    var intersection = simple.reduce(function(r, e) { // get the intersection of the current array and compareArray (simple)
      if(b.indexOf(e) != -1) // if the current element of the current array is also in the compareArray then push it into the intersection array
        r.push(e);
      return r;
    }, []);
    
    res.push(intersection); // push the intersection array into the result array
    return res;
  }, []);
}

console.log(intersect(masterArray, compareArray));
Run codeHide result
+1
source

RegExp ( compareArray ).
: String.prototype.match(), Array.prototype.join(), Array.prototype.map()

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]],
    compareArray = [9,11,13,15,1,2,5,6];

function intersect(compareArray, masterArray) {
    var pattern = new RegExp('\\b(' + compareArray.join('|') + ')\\b', 'g'),
        result = [];

    masterArray.forEach(function(v) {
        var matches = v.join(' ').match(pattern);
        if (matches.length) result.push(matches.map(Number));
    });

    return result;
}

console.log(intersect(compareArray, masterArray));
Hide result
+1

:

var masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]]
var compareArray = [9,11,13,15,1,2,5,6]


function intersect(a, b) {
  var temp = [];
   k = 0;
   for(i = 0; i < b.length; i++){
      temp1 = []
      for(j=0;j<b[i].length;j++){
         if($.inArray(b[i][j], a) > -1){
            temp1.push(b[i][j]);

         }
      }
      temp.push(temp1);
   }
   return temp;
}

console.log(intersect(compareArray, masterArray));

jsfiddle.

, .

0

ES6:

function intersect(a, b) {
    a = new Set(a); // for faster lookup
    return b.map(c => c.filter(a.has.bind(a))).filter(Boolean);
}
// Demo
const masterArray = [[1,2,5,6],[5,13,7,8],[9,11,13,15],[13,14,15,16],[1,9,11,12]];
const compareArray = [9,11,13,15,1,2,5,6];
console.log(intersect(compareArray, masterArray).join('\n'));
Hide result

.filter(Boolean) , , .

Set , Set ( has) .

0

masterArray.

var masterArray = [[1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12]],
    compareArray = [9, 11, 13, 15, 1, 2, 5, 6],
    result = masterArray.map(function (a) {
        return a.filter(function (b) {
            return compareArray.indexOf(b) !== -1;
        });
    });

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hide result

ES6

var masterArray = [[1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12]],
    compareArray = [9, 11, 13, 15, 1, 2, 5, 6],
    result = masterArray.map(a => a.filter(b => compareArray.includes(b)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Hide result
0
source

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


All Articles