Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does lodash's filter function take a context?

I've had a look at the lodash filter documentation and it's unclear whether the third parameter is a context.

I'm using a cytoscape plugin (dagre) and it seems to pass this as a 3 argument. When I pause the execution before the filter method is called, this is defined. But within the call this is undefined.

I had a look at the underscore filter documentation and it seems to take a third argument as a context. So I'm kinda guessing that the plugin originally used underscore then maybe changed to lodash. The project I'm working on is using lodash.

I can't understand why this is null at that point in my could. It could be project specific but I just want to be clear on the third parameter of lodash's filter.

Is the definition of lodash's filter exactly the same as underscore's filter? It does not seem so from the documentation.

like image 582
Touch Avatar asked Dec 12 '17 09:12

Touch


1 Answers

Well you can always define your own context using Function.prototype.bind.

_.filter([…], 
  function (o) {
    console.log(this.id); //100
    //than return something based on o
    return o.active
  }.bind({id: 100})
);

Doc on mdn

like image 59
philipp Avatar answered Sep 28 '22 20:09

philipp