用下划线或下划线表示(下划线,lodash或任何其他解决方案) [英] underscore where with or condition (underscore, lodash or any other solution)

查看:146
本文介绍了用下划线或下划线表示(下划线,lodash或任何其他解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个mixin,用_.where

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();
   }
});

现在我可以像这样使用它,它的工作方式有点像sql查询

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

但是我想找到一个更好的方法来调用_.or({a:1}),现在我必须每次都传递arr _.or(arr,{ a:1}),因为链接或因先前执行的函数而获得第一个对象。

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.

有没有办法在链式mixin函数中获取整个数组?

Is there any way to get entire array in chained mixin function?

我想在下面返回与上述实现相同的结果。 (就像一个完美的SQL查询)

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}]

我的主要目标是通过下划线,但实际上任何其他方式,如lodash甚至其他一些库或解决方案也可以。此外,我们不需要使用链接,我也尝试使用compose,但到目前为止还没有运气。我想要一个在最小线条上完美运行的解决方案。鼓励任何其他建议使其变得更好。

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.

jsFiddle

推荐答案

我不认为扩展下划线是正确的方式,Underscore的链接不是为此而设计的。你可以弯曲它,但是要花费你想要的空间。

I don't think extending underscore is the right way, Underscore's chaining is not desinged for this. You may "bend" it, but at cost of your desired shortness.

下划线的链接是一个流操作。 chain()将传递的对象内部存储在闭包中,并返回Underscore接口,其中每个方法都绑定到该对象。应用过滤器函数时,将返回结果并将其内部存储在结果对象中。然后 value()调用将返回内部引用。

Underscore's chaining is a "stream operation". chain() stores the passed object internally in a closure and returns the Underscore interface where each method is bound to that object. When a filter function is applied, the result is returned and internally stored in a result object. The value() call will then return the internal reference.

您可以使用所谓的流畅界面解决此问题:

You can solve this using a so called fluent interface:

// Fluent interface to filter an array with chained, alternative conditions.
// Usage: whereOr([...]).or({...}).or({...}).end()
//        whereOr([...], {...}).or({...}).end()
// Objects are _.where() conditions to subsequently apply to the array.
function whereOr(arr, condition) {
    var result = [],
        iface;

    iface =  {
        or: function(subcondition) {
            result = result.concat(_.where(arr, subcondition));
            return iface;
        },
        end: function() {
            return _.union(result);
        }
    };

    if (condition) {
        return iface.or(condition);
    }

    return iface;
}

然后你可以做

var arr = [{a:1,b:4}, {a:5}, {a:6}, {a:11}];
whereOr(arr).or({a:1}).or({a:11}).or({a:2}).end();

如果您愿意,可以将此功能混合到Underscore中,当然:

If you like, you could mix this function into Underscore, of course:

_.mixin({
    whereOr: whereOr
});



工作原理



何时 whereOr()被调用它建立一个范围,保存对原始 arr 的引用。它返回一个提供流畅函数或() end()的对象,它们都可以访问初始数组。当 end()将返回结果时,或()将应用Userscore的 where() 到内部引用并将结果添加到结果。然后它再次返回接口对象。这种方式或()提供了另一种过滤的可能性,直到 end()返回工会化结果。在此示例中, whereOr()可选择接受第一个过滤条件作为第二个参数。

How it works

When whereOr() is called it establishes a scope that holds the reference to the original arr. It returns an object that provides the fluent functions or() and end() which both have access to the inital array. While end() will return the result, or() will apply Userscore's where() to the internal reference and add the result to result. It then returns the interface object again. This way or() provides another possibility to filter until end() returns the unionized result. In this example whereOr() optionally accepts a first filter condition as second argument.

您可以比较 whereOr() with chain() end() with value()

You may compare whereOr() with chain() and end() with value().

jsFiddle

这篇关于用下划线或下划线表示(下划线,lodash或任何其他解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆