This question has already been answered:

Hello, friends, I have a function that checks the elements in the array and returns those that have passed the test. How to change the conditions so that it returns those items that did not pass the test? Simple change == to != Not working

 function areIn(oldAra) { return function(elem) { var returnArr = []; for (var i = 0; i < oldAra.length; i++) { for (var j = 0; j < elem.length; j++) { if(elem[j] == oldAra[i]) { returnArr.push(elem[j]); } } } return returnArr; }; } var arr = [1, 2, 3, 4, 5, 6, 7]; var check = areIn(arr); console.log(check([2, 3, 4, 10])); //[2, 3, 4] 

Reported as a duplicate at Grundy. javascript 5 Sep '17 at 9:49 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    2 answers 2

     function areIn(oldAra) { return function(elem) { var returnArr = [], tmp = {}; for (var i = 0; i < oldAra.length; i++) { for (var j = 0; j < elem.length; j++) { if(elem[j] == oldAra[i]) { tmp[elem[j]] = true; } else { if (!tmp[elem[j]]) { tmp[elem[j]] = false; } } } } for (var key in tmp) { if (!tmp[key]) { returnArr.push(key); } } return returnArr; }; } var arr = [1, 2, 3, 4, 5, 6, 7]; var check = areIn(arr); console.log(check([2, 3, 4, 10])); //[10] 

       function areIn(oldAra) { return function(elem) { return elem.filter(item => oldAra.indexOf(item) < 0); }; } var arr = [1, 2, 3, 4, 5, 6, 7]; var check = areIn(arr); console.log(check([2, 3, 4, 10])); //[10]