js 模拟扩展 c# 的 linq 表达式 分享 linq to js

hi :如下是  linq to js  的分享   ,如有雷同 纯属巧合

 

(function () {
    Array.prototype.first = function (predicate, def) {

        var l = this.length;

        if (!predicate) return l ? this[0] : def == null ? null : def;

        for (var i = 0; i < l; i++)

            if (predicate(this[i], i, this))

                return this[i];

        return def == null ? null : def;

    };
    Array.prototype.where = Array.prototype.filter || function (predicate, context) {

        context = context || window;

        var arr = [];

        var l = this.length;

        for (var i = 0; i < l; i++)

            if (predicate.call(context, this[i], i, this) === true) arr.push(this[i]);

        return arr;

    };
    Array.prototype.select = Array.prototype.map || function (selector, context) {

        context = context || window;

        var arr = [];

        var l = this.length;

        for (var i = 0; i < l; i++)

            arr.push(selector.call(context, this[i], i, this));

        return arr;

    };
})();

使用方法:list.first(function (t) { return t.Value.trim() == city.trim(); }); 或
list.first((t)=> { return t.Value.trim() == city.trim(); });

  

posted @ 2018-03-30 10:54  谢冰  阅读(171)  评论(0编辑  收藏  举报