4

I tried looking at a similar question in stack overflow but it didn't cater to exactly what I'm trying to do and I keep trying to make it work but continuously get the wrong results. At this point I'm simply 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 would be

[9,11,13,15]
[1,2,5,6]
[5,13]
[13,15]
6
  • Show (add to your question) your best effort so far and explain where and why you are stuck. Commented Feb 11, 2017 at 19:49
  • Check the output again! I think you forgot one! Commented Feb 11, 2017 at 19:50
  • 3
    why [1,9,11] is not in the final result? Commented Feb 11, 2017 at 19:51
  • you have 5 sub arrays to return 4 outputs.. How so.. Commented Feb 11, 2017 at 19:58
  • Sorry, I missed typing that one, but yes. [1,9,11] would be one of the outputs as well. Commented Feb 12, 2017 at 16:00

5 Answers 5

1

Use Array.prototype.reduce to get an array of all the 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));

Sign up to request clarification or add additional context in comments.

Comments

1

The solution using RegExp object with specific regex pattern(converting compareArray numbers into regex alternation group items).
Additional functions used: 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));

Comments

0

As per your logic, you have wrong output in your question :

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));

Check this jsfiddle.

Give it a try, this should work.

Comments

0

You could use this ES6 function:

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'));

The .filter(Boolean) part is optional, and only needed to exclude arrays that have no matching numbers at all, which otherwise would be represented by empty arrays in the result.

Using Set will improve the performance, as lookup in a Set (with has) can be done in near-constant time.

Comments

0

You could map the filtered result of the 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; }

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.