For the following code:
var arrays = [
['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'],
['taco', 'fish', 'apple', 'pizza'],
['banana', 'pizza', 'fish', 'apple']
];
var result = arrays.shift().filter(
function(v) {return arrays.every(
function(a) {return a.indexOf(v) !== -1;});
});
Here what's happening statement by statement:
var result = arrays.shift().filter(
...
});
For arrays.shift() the first array element ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'] is returned, this is used as the base for the result. The .filter() method will modify this array.
function(v) {return arrays.every(
...
});
Which means for every array in the 2D array arrays, in this case v represents each of the elements in the base array ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza']. While a means each of the arrays inside arrays.
function(a) {return a.indexOf(v) !== -1;});
Finally this means that for all they arrays (a's) if it exist inside that base array v's then it returns true, otherwise if the element inside ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'] isn't in the other arrays then it returns false and that element is removed.
In other words all for each element in ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco','pizza'], there must be a matching element for the other arrays inside arrays.