Construct an 2D array of contiguous elements comparing master and incident arrays
The code could be reduced to a single line by utilizing Array.prototype.reduce()



const mainArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
const compArray = [1, 2, 3, 6, 7, 11, 12, 13, 15];

const finalArray = mainArray.reduce((a, b) =&gt; {
  // `c` is boolean result of checking if `compArray` includes `b`
  // `x` is first, and if matching elements found, last array in `finalResult`
  const [c, x] = [compArray.includes(b), a[a.length - 1]];
  // if `c` push `b` to last array of `a`
  if (c) x.push(b)
  // else if `x.length` push a new array to `a`
  else if (x.length) a.push([]);
  // return `a`
  return a;
}, [[]]); // pass initial array containing single array `a` to `reduce`

console.log(finalArray);