当前支持的方法,陆续添加方法
完整代码
文字版(复制粘贴可用)
(function () {
function checkFunction(func) {
if (typeof func !== "function")
throw "参数类型不是function错误";
}
var Queryable = function (data) {
var _data = [];
var _self = this;
if (data instanceof Array) {
_data = data;
} else {
throw "Queryable不支持非数组类型";
}
//循环执行
_self.forEach = function (callback) {
for (var i = 0; i < _data.length; i++) {
if (callback(_data[i], i) === true) break;
}
}
//求和
_self.sum = function (selector) {
checkFunction(selector);
var s = 0;
this.forEach(x => {
s += selector(x);
});
return s;
}
//求最大值
_self.max = function (selector) {
checkFunction(selector);
var max = undefined;
this.forEach(x => {
var v = selector(x)
max = max > v ? max : (max = v);
});
return max;
}
//求最小值
_self.min = function (selector) {
checkFunction(selector);
var min = undefined;
this.forEach(x => {
var v = selector(x)
min = min < v ? min : (min = v);
});
return min;
}
//求平均值
_self.averate = function (selector) {
checkFunction(selector);
if (_data.length == 0) return undefined;
var sum = this.sum(selector);
return sum / _data.length;
}
//查询符合条件的元素集合
_self.where = function (selector) {
checkFunction(selector);
var arrry = [];
this.forEach(x => {
if (selector(x)) {
arrry.push(x);
}
});
return new Queryable(arrry);
}
//转为数组
_self.toArray = function () {
var array = []
this.forEach(x => array.push(x));
return array;
}
//在指定索引的元素
_self.elementAt = function (index) {
return _data[index];
}
//元素索引
_self.indexOf = function (elm) {
return _data.indexOf(elm);
}
//返回符合条件的首元素,无符合条件则返回undefined
_self.firstOrDefault = function (selector) {
checkFunction(selector);
var one = undefined;
this.forEach(x => {
if (selector(x)) {
one = x;
return true;
}
});
return one;
}
//返回符合条件的末元素,无符合条件则返回undefined
_self.lastOrDefault = function (selector) {
checkFunction(selector);
for (var i = _data.length - 1; i >= 0; i--) {
if (selector(_data[i])) {
return _data[i];
}
}
return undefined;
}
//查找一个元素
_self.findOne = function (selector) {
checkFunction(selector);
var one = undefined;
this.forEach(x => {
if (selector(x)) {
one = x;
return true;
}
});
return one;
}
//查找符合条件的元素并返回新的集合
_self.select = function (selector) {
checkFunction(selector);
var array = [];
this.forEach(item => {
array.push(selector(item));
});
return new Queryable(array);
}
//升序排序
_self.orderBy = function (selector) {
checkFunction(selector);
var compare = function (a, b) {
return selector(a) > selector(b) ? 1 : -1;
}
_data.sort(compare);
return this;
}
//降序排序
_self.orderByDesc = function (selector) {
checkFunction(selector);
var compare = function (a, b) {
return selector(a) > selector(b) ? -1 : 1;
}
_data.sort(compare);
return this;
}
//判断是否全部元素满足条件
_self.all = function (predicate) {
checkFunction(predicate);
for (let item of _data) {
if (predicate(item) === false) return false;
}
return true;
}
//判断是否有元素满足条件
_self.any = function (predicate) {
checkFunction(predicate);
for (let item of _data) {
if (predicate(item)) return true;
}
return false;
}
//加入元素
_self.add = function (item) {
_data.push(item);
return this;
}
//加入多个元素
_self.addRange = function (array) {
for (let item of array) {
_data.push(item);
}
return this;
}
//删除元素
_self.remove = function (item) {
var index = this.indexOf(item);
if (index > -1) {
_data.splice(index, 1);
}
}
//加入元素,如果已存在则更新元素
_self.addOrUpdate = function (item) {
var index = this.indexOf(item);
if (index > -1) _data[index] = item;
else {
this.add(item);
}
return this;
}
//求满足条件元素数量
_self.count = function (selector) {
if (arguments.length == 0) {
return _data.length;
}
checkFunction(selector);
var count = 0;
this.forEach(x => {
if (selector(x)) count++;
});
return count;
}
//排重并返回新集合
_self.distinctBy = function (keySelector) {
checkFunction(keySelector);
const array = [];
const map = new Map()
this.forEach(item => {
var val = keySelector(item);
if (!map.has(val)) {
map.set(val, true)
array.push(item);
}
})
return Linq.from(array);
}
}
window.Linq = {
from: function (data) {
return new Queryable(data);
}
}
Array.prototype.toLinq = function () {
return Linq.from(this);
}
})();
代码测试
喜欢的朋友留个关注,谢谢!