- 浏览: 178625 次
- 性别:
文章分类
最新评论
LINQ IN js
Reference:http://www.codeproject.com/Articles/603742/LINQ-for-JavaScript
LINQ-JS
(function () { // Default methods function DefaultEqualityComparer(a, b) { return a === b || a.valueOf() === b.valueOf(); }; function DefaultSortComparer(a, b) { if (a === b) return 0; if (a == null) return -1; if (b == null) return 1; if (typeof a == "string") return a.toString().localeCompare(b.toString()); return a.valueOf() - b.valueOf(); }; function DefaultPredicate() { return true; }; function DefaultSelector(t) { return t; }; // Selectors 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; }; Array.prototype.selectMany = function (selector, resSelector) { resSelector = resSelector || function (i, res) { return res; }; return this.aggregate(function (a, b) { return a.concat(selector(b).select(function (res) { return resSelector(b, res) })); }, []); }; Array.prototype.take = function (c) { return this.slice(0, c); }; Array.prototype.skip = Array.prototype.slice; 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.last = function (predicate, def) { var l = this.length; if (!predicate) return l ? this[l - 1] : def == null ? null : def; while (l-- > 0) if (predicate(this[l], l, this)) return this[l]; return def == null ? null : def; }; Array.prototype.union = function (arr) { return this.concat(arr).distinct(); }; Array.prototype.intersect = function (arr, comparer) { comparer = comparer || DefaultEqualityComparer; return this.distinct(comparer).where(function (t) { return arr.contains(t, comparer); }); }; Array.prototype.except = function (arr, comparer) { if (!(arr instanceof Array)) arr = [arr]; comparer = comparer || DefaultEqualityComparer; var l = this.length; var res = []; for (var i = 0; i < l; i++) { var k = arr.length; var t = false; while (k-- > 0) { if (comparer(this[i], arr[k]) === true) { t = true; break; } } if (!t) res.push(this[i]); } return res; }; 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; }; Array.prototype.zip = function (arr, selector) { return this .take(Math.min(this.length, arr.length)) .select(function (t, i) { return selector(t, arr[i]); }); }; 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; }; Array.prototype.lastIndexOf = Array.prototype.lastIndexOf || function (o, index) { var l = Math.max(Math.min(index || this.length, this.length), 0); while (l-- > 0) if (this[l] === o) return l; return -1; }; Array.prototype.remove = function (item) { var i = this.indexOf(item); if (i != -1) this.splice(i, 1); }; Array.prototype.removeAll = function (predicate) { var item; var i = 0; while ((item = this.first(predicate)) != null) { i++; this.remove(item); } return i; }; 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); }; Array.prototype.orderByDescending = function (selector, comparer) { comparer = comparer || DefaultSortComparer; return this.orderBy(selector, function (a, b) { return -comparer(a, b) }); }; Array.prototype.innerJoin = function (arr, outer, inner, result, comparer) { comparer = comparer || DefaultEqualityComparer; var res = []; this.forEach(function (t) { arr.where(function (u) { return comparer(outer(t), inner(u)); }) .forEach(function (u) { res.push(result(t, u)); }); }); return res; }; Array.prototype.groupJoin = function (arr, outer, inner, result, comparer) { comparer = comparer || DefaultEqualityComparer; return this .select(function (t) { var key = outer(t); return { outer: t, inner: arr.where(function (u) { return comparer(key, inner(u)); }), key: key }; }) .select(function (t) { t.inner.key = t.key; return result(t.outer, t.inner); }); }; 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; }; Array.prototype.toDictionary = function (keySelector, valueSelector) { var o = {}; var l = this.length; while (l-- > 0) { var key = keySelector(this[l]); if (key == null || key == "") continue; o[key] = valueSelector(this[l]); } return o; }; // Aggregates Array.prototype.aggregate = Array.prototype.reduce || function (func, seed) { var arr = this.slice(0); var l = this.length; if (seed == null) seed = arr.shift(); for (var i = 0; i < l; i++) seed = func(seed, arr[i], i, this); return seed; }; 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; }; 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; }; Array.prototype.sum = function (s) { s = s || DefaultSelector; var l = this.length; var sum = 0; while (l-- > 0) sum += s(this[l]); return sum; }; // Predicates 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.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]); }; 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]); }; Array.prototype.takeWhile = function (predicate) { predicate = predicate || DefaultPredicate; var l = this.length; var arr = []; for (var i = 0; i < l && predicate(this[i], i) === true ; i++) arr.push(this[i]); return arr; }; Array.prototype.skipWhile = function (predicate) { predicate = predicate || DefaultPredicate; var l = this.length; var i = 0; for (i = 0; i < l; i++) if (predicate(this[i], i) === false) break; return this.skip(i); }; 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; }; // Iterations 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); }; Array.prototype.defaultIfEmpty = function (val) { return this.length == 0 ? [val == null ? null : val] : this; }; Array.range = function (start, count) { var arr = []; while (count-- > 0) { arr.push(start++); } return arr; }; }());
usage :
1.save the codes above as LINQ_JS.js
2.use it just as in C# ,but pass function to it .
<html> <head></head> <body> <script src="LINQ_JS.js" ></script> <script type="text/javascript">
var arr = [1,2,3,4]; var newArr = arr.where(function(a){return a> 2}); var ret = ""; for(var i = 0 ;i < newArr.length;i ++) ret += newArr[i] + ","; alert(ret);
</script> </body> </html>
相关推荐
Millions of people worldwide have used Deitel books and LiveLessons videos to master C#, C++, Java™, C, iPhone app development, Internet and web programming, JavaScript, XML, Visual Basic®, Visual ...
LINQ定义了大约40个查询操作符,如select、from、in、where、group 以及order by,借助于LINQ技术,C#语音可以实现,javascript也可以实现
没有使用for-in循环,因此代码也可以针对ES5 许多功能尚未经过全面测试。 由于语言限制,方法singleOrDefault , firstOrDefault , elementAtOrDefault , lastOrDefault , defaultIfEmpty已替换为...
While ASP.NET 3.5 boasts server controls like the ListView and the incredibly flexible GridView, it also includes advancements in AJAX technology combined with JavaScript® debugging features in ...
对 JavaScript 中原生数组、对象进行扩展, 提供了一些对数据的操作方法.包括对数组结构、树形结构、对象结构等数据进行 查询,排序,连接,合并,分组,分段,转换,遍历 等一系列功能.可以用精炼和易懂的代码实现比较复杂...
Over 80 recipes to make the most of JSON in your desktop, server, web, and mobile applications ...Chapter 9 : Querying JSON with JSONPath and LINQ Chapter 10 : JSON on Mobile Platforms
链表javascript中的链表实现请通过 mocha 测试文档npm 测试链表堆栈: ✓ allows adding elements through array inside constructor ✓ allows adding elements ✓ allows taking out elements at top ✓ has an ...
Applied ASP.NET 4 in Context 952 pages Publisher: Apress; 1 edition (September 12, 2011) Language: English ISBN-10: 1430234679 ISBN-13: 978-1430234678 What you’ll learn •Understand the ...
const query = linq `from n in [0, 1, 2] select n + 1` console . log ( [ ... query ] ) 概述 LinqBox是JavaScript语言集成查询的实验性实现。 它被编写为JavaScript生成器的抽象,它允许通过LINQ查询表达式语法...
一种用于查询 javascript 数组的 javascript 查询语言。 用 假设有一个数组 a = [1, 2, 3, 4, 5]; 在查询之前,需要将数组导入到 JSIQ 中。 这样做: JSIQ.import("a", a); 您可以通过如下查询使用 jsiq.js: ...
This includes user controls, customer server controls, client–side JavaScript, and GDI+. Web services. In an increasingly connected world, working with web services grows in importance. This book ...
This includes user controls, customer server controls, client–side JavaScript, and GDI+. Web services. In an increasingly connected world, working with web services grows in importance. This book ...
Grouping, Ordering, and Other Advanced Queries in LINQ to SQL 951 Displaying Generated SQL 954 Data Binding with LINQ to SQL 958 Updating Bound Data with LINQ to SQL 965 Summary 966 ...
Millions of people worldwide have used Deitel books and LiveLessons videos to master C#, C++, Java™, C, iPhone app development, Internet and web programming, JavaScript, XML, Visual Basic®, Visual ...
When typing a parameter in a C# LINQ predicate function, focus is no longer given to the suggestion list, matching the default VS 2008 behavior. (case=16277) C++ keyword __restrict is recognized. ...
* When typing a parameter in a C# LINQ predicate function, focus is no longer given to the suggestion list, matching the default VS 2008 behavior. (case=16277) * C++ keyword __restrict is recognized...
foreach( object o in array )arrayList.Add(o); 3.datagrid.datasouse可以连接什么数据源 [dataset,datatable,dataview] dataset,datatable,dataview , IList 4.概述反射和序列化 反射:程序集包含模块,而模块...
) 将LINQ 查询转换为在 MongoDB 中运行的JavaScript代码考虑这个 LINQ 查询: var query = from p in mongo.People where p.Age > 20 && p.Name.Length >= 2 orderby p.Age descending select p;打电话 Console....
IEnumerable infact,源于将带入JavaScript环境的想法。 Linq是一种很棒的查询数据的技术,JavaScript应该有它。 另外,我们希望保持的优势并对其进行改进。 在IEnumerable中,不仅集合是不可变的,而且其内容也是...
CHAPTER 13 LINQ 531 CHAPTER 14 XML 587 PART 3 Building ASP.NET Websites CHAPTER 15 User Controls 645 CHAPTER 16 Themes and Master Pages 665 CHAPTER 17 Website Navigation 695 CHAPTER 18 Website ...