Select
遍历元素下的每一个元素,调用JS.Call方法返回数据。
1 Array.prototype.select = Array.prototype.map || function (selector, context) { 2 context = context || window; 3 var arr = []; 4 var l = this.length; 5 for (var i = 0; i < l; i++) 6 arr.push(selector.call(context, this[i], i, this)); 7 return arr; 8 }; 9 10 11 var arr = [1, 2, 3, 4, 5]; 12 var doubled = arr.select(function(t){ return t * 2 }); 13 14 Select
SelectMany Array.prototype.selectMany = function (selector, resSelector) { resSelector = resSelector || function (i, res) { return res; }; return this.aggregate(function (a, b, i) { return a.concat(selector(b, i).select(function (res) { return resSelector(b, res) })); }, []); }; var arr = [{Name:"A", Values:[1, 2, 3, 4]}, {Name:"B", Values:[5, 6, 7, 8]}]; var res1 = arr.selectMany(function(t){ return t.Values }); var res2 = arr.selectMany(function(t){ return t.Values }, function(t, u){ return {Name:t.Name, Val:u}});
Take
Array.prototype.take = function (c) { return this.slice(0, c); }; var arr = [1, 2, 3, 4, 5]; var res = arr.take(2);
Skip
Array.prototype.skip = function (c) { return this.slice(c); }; var arr = [1, 2, 3, 4, 5]; var res = arr.skip(2);
First
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; }; var arr = [1, 2, 3, 4, 5]; var t1 = arr.first(); var t2 = arr.first(function(t){ return t > 2 }); var t3 = arr.first(function(t){ return t > 10 }, 10); //默认值是10
1 Union 2 3 Array.prototype.union = function (arr) { 4 return this.concat(arr).distinct(); 5 }; 6 7 var arr1 = [1, 2, 3, 4, 5]; 8 var arr2 = [5, 6, 7, 8, 9]; 9 var res = arr1.union(arr2);
Distinct 找出不重复的数据。当有重复元素是只push一个元素进集合。 Array.prototype.distinct = function (comparer) { var arr = []; var l = this.length; for (var i = 0; i < l; i++) { if (!arr.contains(this[i], comparer)) arr.push(this[i]); } return arr; }; var arr1 = [1, 2, 2, 3, 3, 4, 5, 5]; var res1 = arr.distinct(); // [1, 2, 3, 4, 5] var arr2 = [{Name:"A", Val:1}, {Name:"B", Val:1}]; var res2 = arr2.distinct(function(a, b){ return a.Val == b.Val }); //返回[{Name:"A", Val:1}]
IndexOf 查找指定的值第一次出现的位置。 Array.prototype.indexOf = Array.prototype.indexOf || function (o, index) { var l = this.length; for (var i = Math.max(Math.min(index, l), 0) || 0; i < l; i++) if (this[i] === o) return i; return -1; }; var arr = [1, 2, 3, 4, 5]; var index = arr.indexOf(2); // 1
Remove
Array.prototype.remove = function (item) { var i = this.indexOf(item); if (i != -1) this.splice(i, 1); }; var arr = [1, 2, 3, 4, 5]; arr.remove(2); // [1, 3, 4, 5]
OrderBy Array.prototype.orderBy = function (selector, comparer) { comparer = comparer || DefaultSortComparer; var arr = this.slice(0); var fn = function (a, b) { return comparer(selector(a), selector(b)); }; arr.thenBy = function (selector, comparer) { comparer = comparer || DefaultSortComparer; return arr.orderBy(DefaultSelector, function (a, b) { var res = fn(a, b); return res === 0 ? comparer(selector(a), selector(b)) : res; }); }; arr.thenByDescending = function (selector, comparer) { comparer = comparer || DefaultSortComparer; return arr.orderBy(DefaultSelector, function (a, b) { var res = fn(a, b); return res === 0 ? -comparer(selector(a), selector(b)) : res; }); }; return arr.sort(fn); }; var arr = [{Name:"A", Val:1}, {Name:"a", Val:2}, {Name:"B", Val:1}, {Name:"C", Val:2}]; var res1 = arr.orderBy(function(t){ return t.Name }); var res2 = arr.orderBy(function(t){ return t.Name }, function(a, b){ if(a.toUpperCase() > b.toUpperCase()) return 1; if(a.toUpperCase() < b.toUpperCase()) return -1; return 0; });
OrderByDescending
Array.prototype.orderByDescending = function (selector, comparer) { comparer = comparer || DefaultSortComparer; return this.orderBy(selector, function (a, b) { return -comparer(a, b) }); }; var arr = [{Name:"A", Val:1}, {Name:"a", Val:2}, {Name:"B", Val:1}, {Name:"C", Val:2}]; var res = arr.orderByDescending(function(t){ return t.Name });
GroupBy
Array.prototype.groupBy = function (selector, comparer) { var grp = []; var l = this.length; comparer = comparer || DefaultEqualityComparer; selector = selector || DefaultSelector; for (var i = 0; i < l; i++) { var k = selector(this[i]); var g = grp.first(function (u) { return comparer(u.key, k); }); if (!g) { g = []; g.key = k; grp.push(g); } g.push(this[i]); } return grp; };
Javascript 聚合
Min Array.prototype.min = function (s) { s = s || DefaultSelector; var l = this.length; var min = s(this[0]); while (l-- > 0) if (s(this[l]) < min) min = s(this[l]); return min; }; var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]; var min1 = arr.min(); // 1 var arr2 = [{Name:"A", Val:1}, {Name:"B", Val:2}]; var min2 = arr2.min(function(t){ return t.Val }); // 1
Max
Array.prototype.max = function (s) { s = s || DefaultSelector; var l = this.length; var max = s(this[0]); while (l-- > 0) if (s(this[l]) > max) max = s(this[l]); return max; }; var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]; var max1 = arr.max(); // 8 var arr2 = [{Name:"A", Val:1}, {Name:"B", Val:2}]; var max2 = arr2.max(function(t){ return t.Val }); // 2
Sum
Array.prototype.sum = function (s) { s = s || DefaultSelector; var l = this.length; var sum = 0; while (l-- > 0) sum += s(this[l]); return sum; }; var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]; var sum1 = arr.sum(); // 36 var arr2 = [{Name:"A", Val:1}, {Name:"B", Val:2}]; var sum2 = arr2.sum(function(t){ return t.Val }); // 3
Javascript 条件查询
Where 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; }; var arr = [1, 2, 3, 4, 5]; var res = arr.where(function(t){ return t > 2 }) ; // [3, 4, 5]
Any
Array.prototype.any = function (predicate, context) { context = context || window; var f = this.some || function (p, c) { var l = this.length; if (!p) return l > 0; while (l-- > 0) if (p.call(c, this[l], l, this) === true) return true; return false; }; return f.apply(this, [predicate, context]); }; var arr = [1, 2, 3, 4, 5]; var res1 = arr.any(); // true var res2 = arr.any(function(t){ return t > 5 }); // false
All
Array.prototype.all = function (predicate, context) { context = context || window; predicate = predicate || DefaultPredicate; var f = this.every || function (p, c) { return this.length == this.where(p, c).length; }; return f.apply(this, [predicate, context]); }; var arr = [1, 2, 3, 4, 5]; var res = arr.all(function(t){ return t < 6 }); // true
Contains
Array.prototype.contains = function (o, comparer) { comparer = comparer || DefaultEqualityComparer; var l = this.length; while (l-- > 0) if (comparer(this[l], o) === true) return true; return false; }; var arr1 = [1, 2, 3, 4, 5]; var res1 = arr.contains(2); // true var arr2 = [{Name:"A", Val:1}, {Name:"B", Val:1}]; var res2 = arr2.contains({Name:"C", Val:1}, function(a, b){ return a.Val == b.Val }) ; // true
Javasciprt 迭代
ForEach Array.prototype.forEach = Array.prototype.forEach || function (callback, context) { context = context || window; var l = this.length; for (var i = 0; i < l; i++) callback.call(context, this[i], i, this); }; var arr = [1, 2, 3, 4, 5]; arr.forEach(function(t){ if(t % 2 ==0) console.log(t); });
DefaultIfEmpty
Array.prototype.defaultIfEmpty = function (val) { return this.length == 0 ? [val == null ? null : val] : this; }; var arr = [1, 2, 3, 4, 5]; var res = arr.where(function(t){ return t > 5 }).defaultIfEmpty(5); // [5]
原文:http://www.cnblogs.com/logep/p/3525812.html