2

How can I pass an array to the filter method in JavaScript?

For example I want to filter an array with another array. Now I have my code working but I am not passing the array, my filter array has a global scope. Is there a way to pass the array to have a cleaner code?

var array = [1, 2, 3, 4, 5];
var filterNumbers = [1, 4];

var result = array.filter(filterData);

function filterData(value) {
  return filterNumbers.indexOf(value) === -1;
}
Jose Angel
  • 328
  • 2
  • 16
  • possible duplicate of [jQuery/JavaScript: Filter array with another array](http://stackoverflow.com/questions/18894241/jquery-javascript-filter-array-with-another-array) – Dalorzo Aug 31 '15 at 18:52
  • Why would you want to pass an array into another function? This is how OOP works. You use the `.filter` method which belongs to Array to filter the array. This is already very clean (cleanest imo) and easy to understand. – Derek 朕會功夫 Aug 31 '15 at 18:54
  • Not a direct answer, but I would strongly recommend that you not try to implement this yourself. Projects like http://underscorejs.org/ or https://lodash.com/ have lots of functions to help you do these sorts of things and those projects put a lot of time and effort into making them perform well. – CodingGorilla Aug 31 '15 at 18:55
  • possible duplicate of [Simplest code for array intersection in javascript](http://stackoverflow.com/questions/1885557/simplest-code-for-array-intersection-in-javascript) – njzk2 Aug 31 '15 at 18:55

5 Answers5

4

You can do this by using partial application (fiddle):

function filterUsing(filterByArray) { // get the array to filter by
    return function(value) { // return a filtering function
        return filterByArray.indexOf(value) === -1;
    };
}

var filterFunc = filterUsing(filterNumbers); // get the function using your filterNumbers array

var result = array.filter(filterFunc);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Thanks for your fast answer, your code works for me. It looks cleaner and well explained. It helped me a lot. – Jose Angel Aug 31 '15 at 19:06
1

you could use an anonymous function :

var array = [1, 2, 3, 4, 5];
var filterNumbers = [1, 4];
var result = array.filter(function(value) {
    return filterNumbers.indexOf(value) === -1;
});
taxicala
  • 21,408
  • 7
  • 37
  • 66
1

You can create a function to return the callback that is being used to filter the array. That way you break the dependency between the filterNumbers and the filter function itself.

See:

var array = [1, 2, 3, 4, 5];
var filterNumbers = [1, 4];

var result = array.filter(myFilter(filterNumbers));

function myFilter(mynumbers){ 
  return function filterData(value) {
    return mynumbers.indexOf(value) === -1;
  }
}

document.getElementById("test").innerHTML = JSON.stringify(result);
<div id="test"></div>

I hope it helps. Happy coding!

rdgfuentes
  • 346
  • 1
  • 8
0

Short answer: NO.

The filter method expects a callback function as argument. Source: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/filtro

Also it makes no sense that the javascript would figure out what you want to do by just passing an array. To make the code cleaner you can pass an anonymous function:

var array = [1, 2, 3, 4, 5];
var filterNumbers = [1, 4];

var result = array.filter(function() {
 return filterNumbers.indexOf(value) === -1;
});
Pietro Coelho
  • 1,894
  • 1
  • 26
  • 34
0

I think your best bet is a function that returns a filtering function:

function filterFN(numbers) {
  return function(value) { return number.indexOf(value) === -1; };
}

var array = [1, 2, 3, 4, 5];
var result = array.filter(filterFN([1,4]));
Christian Fritz
  • 20,641
  • 3
  • 42
  • 71