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;
}
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;
}
Share
Improve this question
edited Aug 31, 2015 at 18:58
Jose Angel
asked Aug 31, 2015 at 18:49
Jose AngelJose Angel
3284 silver badges18 bronze badges
4
- possible duplicate of jQuery/JavaScript: Filter array with another array – Dalorzo Commented Aug 31, 2015 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 朕會功夫 Commented Aug 31, 2015 at 18:54 - Not a direct answer, but I would strongly remend that you not try to implement this yourself. Projects like underscorejs or lodash. 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 Commented Aug 31, 2015 at 18:55
- possible duplicate of Simplest code for array intersection in javascript – njzk2 Commented Aug 31, 2015 at 18:55
5 Answers
Reset to default 4You 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);
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;
});
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!
Short answer: NO.
The filter
method expects a callback function as argument.
Source: https://developer.mozilla/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;
});
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]));