I implemented a mixin to add "or" condition with _.where
var arr = [{a:1,b:4}, {a:5}, {a:6}, {a:11}];
_.mixin({
or: function(obj,arr,condition){
return _.chain(arr).where(condition).union(obj).value();
}
});
now i can use it like this and it works perfectly somewhat like a sql query
_.chain(arr).where({a:1}).or(arr,{a:11,b:3}).or(arr,{a:2}).value();
//returns [{a:1,b:4}]
_.chain(arr).where({a:1}).or(arr,{a:11}).or(arr,{a:2}).value();
//returns [{a:1,b:4},{a:11}]
_.chain(arr).where({a:1}).or(arr,{a:11}).or(arr,{b:4}).value();
//returns [{"a":1,"b":4},{"a":11}] --no duplicates
but I want to find a better way to just call _.or({a:1}), right now I have to pass arr everytime _.or(arr,{a:1}), as chained "or" gets first object as the result of previously executed functions.
Is there any way to get entire array in chained mixin function?
I want below to return the same result as my above implementation. (like a perfect sql query)
_.chain(arr).where({a:1}).or({a:11,b:3}).or({a:2}).value();//returns [{a:1,b:4}]
my prime focus is to get it through underscore, but really any other way like lodash or even some other library or solution will work too. Also it's not required that we use chaining, i am trying with compose as well but so far no luck. I want a solution which work perfectly in minimum lines. Any other suggestion to make it better is encouraged.