125

I want to compare each string in an Array with a given string. My current implementation is:

function startsWith(element) {
    return element.indexOf(wordToCompare) === 0;
}
addressBook.filter(startsWith);

This simple function works, but only because right now wordToCompare is being set as a global variable, but of course I want to avoid this and pass it as a parameter. My problem is that I am not sure how to define startsWith() so it accepts one extra parameter, because I dont really understand how the default parameters it takes are passed. I've tried all the different ways I can think of and none of them work.

If you could also explain how the passed parameters to 'built in' callback functions (sorry, I dont know of a better term for these) work that would be great

agente_secreto
  • 7,959
  • 16
  • 57
  • 83
  • Possible duplicate of [Pass an extra argument to a callback function](https://stackoverflow.com/q/40802071/1048572) – Bergi Apr 24 '21 at 23:10

8 Answers8

179

Make startsWith accept the word to compare against and return a function which will then be used as filter/callback function:

function startsWith(wordToCompare) {
    return function(element) {
        return element.indexOf(wordToCompare) === 0;
    }
}

addressBook.filter(startsWith(wordToCompare));

Another option would be to use Function.prototype.bind [MDN] (only available in browser supporting ECMAScript 5, follow a link for a shim for older browsers) and "fix" the first argument:

function startsWith(wordToCompare, element) {
    return element.indexOf(wordToCompare) === 0;
}

addressBook.filter(startsWith.bind(this, wordToCompare));

I dont really understand how the default parameters it takes are passed

There is nothing special about it. At some point, filter just calls the callback and passes the current element of the array. So it's a function calling another function, in this case the callback you pass as argument.

Here is an example of a similar function:

function filter(array, callback) {
    var result = [];
    for(var i = 0, l = array.length; i < l; i++) {
        if(callback(array[i])) {  // here callback is called with the current element
            result.push(array[i]);
        }
    }
    return result;
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    Ok now I understand. I was trying to pass the parameters directly to the callback function... I really need to work on my JavaScript. Thank you Felix, your answer is very helpful – agente_secreto Oct 13 '11 at 19:27
  • What about passing additional arguments? I tried passing an array of arguments but that seems to fail – geotheory Dec 14 '14 at 10:25
  • @geotheory: what about them? you pass multiple arguments like to any other function. – Felix Kling Dec 14 '14 at 17:04
  • bind(this) after function name along with filter() chaining helped me use .this inside function. Thanks. – Sagar Khatri May 28 '20 at 11:41
  • in the first snippet, where is that `element` coming from, in the `startsWith` function? – Masroor Oct 29 '20 at 06:56
  • @Prime: `addressBook` appears to be an array. `addressBook.filter` calls the provided function for each element of the array passing it as argument. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter . The last example in my answer shows a `.filter` might be implemented. – Felix Kling Oct 29 '20 at 08:36
122

The second parameter of filter will set this inside of the callback.

arr.filter(callback[, thisArg])

So you could do something like:

function startsWith(element) {
    return element.indexOf(this) === 0;
}
addressBook.filter(startsWith, wordToCompare);
Jeff
  • 2,728
  • 3
  • 24
  • 41
  • 8
    I found this is the best answer. – Jeaf Gilbert Jul 17 '16 at 13:05
  • so now the new array will be assigned to wordToCompare object, right? How can access the new array later using the wordToCompare object? – Badhon Jain Jul 19 '16 at 07:27
  • best answer. works perfect for both filter and find. And is per WC3 documention for both: thisValue - Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value – richaa Jun 07 '18 at 14:27
  • What if several arguments are needed? – Tarek Eldeeb Nov 21 '19 at 14:36
  • 3
    @TarekEldeeb just pass an object you make `{one: 'haha', two:'hoho'}` – toddmo Nov 24 '19 at 22:37
  • 2
    This is a great example of how there can be vast differences among answers as to complexity and how convoluted they can be vs how simple they can be – toddmo Nov 24 '19 at 22:39
  • The value of thisArg is undefined for me when I try to use it with ES6. – Erin May 15 '21 at 18:30
21

For those looking for an ES6 alternative using arrow functions, you can do the following.

let startsWith = wordToCompare => (element, index, array) => {
  return element.indexOf(wordToCompare) === 0;
}

// where word would be your argument
let result = addressBook.filter(startsWith("word"));

Updated version using includes:

const startsWith = wordToCompare => (element, index, array) => {
  return element.includes(wordToCompare);
}
technoY2K
  • 2,442
  • 1
  • 24
  • 38
  • There is any way to pass a different parameter from element, index, and array? For example, I want to pass an X variable. – imtk Nov 25 '18 at 13:41
  • @leandrotk in this case "wordToCompare" is the "X" variable to be passed in. – GetBackerZ Dec 04 '19 at 19:41
11
function startsWith(element, wordToCompare) {
    return element.indexOf(wordToCompare) === 0;
}

// ...
var word = "SOMETHING";

addressBook.filter(function(element){
    return startsWith(element, word);
});
James Montagne
  • 77,516
  • 14
  • 110
  • 130
5

You can use the arrow function inside a filter, like this:

result = addressBook.filter(element => element.indexOf(wordToCompare) === 0);

Arrow functions on MDN

An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). Arrow functions are always anonymous. These function expressions are best suited for non-method functions and they can not be used as constructors.

oddRaven
  • 672
  • 1
  • 7
  • 20
1

For anyone wondering why their fat arrow function is ignoring [, thisArg], e.g. why

["DOG", "CAT", "DOG"].filter(animal => animal === this, "DOG") returns []

it's because this inside those arrow functions are bound when the function is created and are set to the value of this in the broader encompassing scope, so the thisArg argument is ignored. I got around this pretty easily by declaring a new variable in a parent scope:

let bestPet = "DOG"; ["DOG", "CAT", "DOG"].filter(animal => animal === bestPet); => ["DOG", "DOG"]

Here is a link to some more reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_separate_this

JKettler
  • 105
  • 1
  • 2
0

based on oddRaven answer and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

i did it 2 different way . 1) using function way . 2) using inline way .

//Here  is sample codes : 

var templateList   = [
{ name: "name1", index: 1, dimension: 1 }  ,
{ name: "name2", index: 2, dimension: 1 }  ,
{ name: "name3", index: 3, dimension: 2 }  ];


//Method 1) using function : 

function getDimension1(obj) {
                if (obj.dimension === 1) // This is hardcoded . 
                    return true;
                else return false;
            } 

var tl = templateList.filter(getDimension1); // it will return 2 results. 1st and 2nd objects. 
console.log(tl) ;

//Method 2) using inline way 
var tl3 = templateList.filter(element => element.index === 1 || element.dimension === 2  ); 
// it will return 1st and 3rd objects 
console.log(tl3) ;
Ashish
  • 77
  • 1
  • 4
0

There is an easy way to use the filter function, access all params, and not over complicate it.

Unless the callback's thisArg is set to another scope filter does not create its own scope, and we can access params within the current scope. We can set 'this' to define a different scope in order to access other values if needed, but by default it is set to the scope it's called from. You can see this being used for Angular scopes in this stack.

Using indexOf is defeating the purpose of filter, and adding more overhead. Filter is already going through the array, so why do we need to iterate through it again? We can instead make it a simple pure function.

Here's a use-case scenario within a React class method where the state has an array called items, and by using filter we can check the existing state:

checkList = (item) => {  // we can access this param and globals within filter
  var result = this.state.filter(value => value === item); // returns array of matching items

  result.length ? return `${item} exists` : this.setState({
    items: items.push(item) // bad practice, but to keep it light
  });
}
DBrown
  • 5,111
  • 2
  • 23
  • 24