var array1 = [1, 2, 3, 4]; var array2 = [1, 2, 4, 6]; var array3 = [1, 2, 4, 8]; // the lodash way var cool = _.intersection(array1, array2, array3); // Pure JS, implementation of Underscore lib, slightly modified: intersection = function(array) { var result = []; var argsLength = arguments.length; for (var i = 0; i < array.length; i++) { var item = array[i]; if (result.indexOf(item) > -1) continue; for (var j = 1; j < argsLength; j++) { if (!(arguments[j].indexOf(item) > -1)) break; } if (j === argsLength) result.push(item); } return result; }; notCool = intersection(array1, array2, array3); //display it on page document.getElementById("cool").innerHTML = cool; document.getElementById("not-cool").innerHTML = notCool;
Lodash result: <p id="cool"></p> Pure JS result: <p id="not-cool"></p>