「分享」JS快捷操作数组的一些方法

(function () {    var _data = [];    var Queryable = function (data) {        if (data instanceof Array) {            _data = data;        } else {            throw "Queryable不支持非数组类型";        }    }    Queryable.prototype.forEach = function (callback) {        for (var obj in _data) {            callback(_data[obj]);        }    }    Queryable.prototype.sum = function (selector) {        if (typeof selector !== "function") return;        var s = 0;        this.forEach(x => {            s += selector(x);        });        return s;    }    Queryable.prototype.max = function (selector) {        if (typeof selector !== "function") return;        var max = undefined;        this.forEach(x => {            var v = selector(x)            max = max > v ? max : (max = v);        });        return max;    }    Queryable.prototype.min = function (selector) {        if (typeof selector !== "function") return;        var min = undefined;        this.forEach(x => {            var v = selector(x)            min = min < v ? min : (min = v);        });        return min;    }    Queryable.prototype.where = function (selector) {        if (typeof selector !== "function") return;        var arrry = [];        this.forEach(x => {            if (selector(x)) {                arrry.push(x);            }        });        return new Queryable(arrry);    }    Queryable.prototype.toArray = function () {        var array = []        for (var i = 0; i < _data.length; i++) {            array.push(_data[i]);        }        return array;    }    Queryable.prototype.elementAt = function (index) {        return _data[index];    }    Queryable.prototype.indexOf = function (elm) {        return _data.indexOf(elm);    }    Queryable.prototype.remove = function (item) {        var index = this.indexOf(item);        if (index > -1) {            _data.splice(index, 1);        }    }    Queryable.prototype.findOne = function (selector) {        if (typeof selector !== "function") return undefined;        for (var obj in _data) {            if (selector(_data[obj])) {                return _data[obj];            }        }        return undefined;    }    Queryable.prototype.select = function (selector) {        if (typeof selector !== "function") this;        var array = [];        for (var obj in _data) {            array.push(selector(_data[obj]));        }        return new Queryable(array);    }    Queryable.prototype.orderBy = function (selector) {        if (typeof selector !== "function") return this;        var compare = function (a, b) {            return selector(a) > selector(b) ? 1 : -1;        }        _data.sort(compare);        return this;    }    Queryable.prototype.orderByDesc = function (selector) {        if (typeof selector !== "function") return this;        var compare = function (a, b) {            return selector(a) > selector(b) ? -1 : 1;        }        _data.sort(compare);        return this;    }    Queryable.prototype.all = function (predicate) {        if (typeof predicate !== "function")            throw "参数类型错误";        for (let item of _data) {            if (predicate(item) === false) return false;        }        return true;    }    Queryable.prototype.any = function (predicate) {        if (typeof predicate !== "function")            throw "参数类型错误";        for (let item of _data) {            if (predicate(item)) return true;        }        return false;    }    Queryable.prototype.add = function (item) {        _data.push(item);        return this;    }    Queryable.prototype.addOrUpdate = function (item) {        var index = this.indexOf(item);        if (index > -1) _data[index] = item;        else {            this.add(item);        }        return this;    }    window.Linq = {        from: function (data) {            return new Queryable(data);        }    };})();

测试:

var myData = [{ name: 'a', number: 1 }, { name: 'b', number: 2 }, { name: 'e', number: 5 }, { name: 'c', number: 3 }, { name: 'd', number: 4 }];console.log("min:" + Linq.from(myData).min(function (x) { return x.number }));console.log("findOne:" + Linq.from(myData).findOne(function (x) { return x.number == 2 }));

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章