Create chain in lodash with custom functions


Is there way to get my own custom function in a chain of lodash. So for example like this:

var l = [1,2,3]
var add = function(a, b){return a+b}

var r =_.chain(l).find(function(a){return a>1}).add(5).value()

=>r = 7


What you look for is a way to extend the lodash prototype. It so nicely turns out that you can do it easily with a mixin utility function. Check here the docs:

In your example it will look like:

var l = [1,2,3];
var  add = function(a, b){return a+b}

    add: add 

var r =_.chain(l).find(function(a){return a>1}).add(5).value()
console.log(r); ==> 7

and here is live sample on fiddle:

Answered By – stride

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published