LIINQ TO JS
记录一下,方便自己查找。。。
自己在开发前端时,对于处理JSON,觉得真是枯燥。处理数据,基本都要循环。 所以我想着前端也能跟后端一样,有Linq来处理我的JSON对象就好了。上网一搜,找到了JSLINQ.js。。实现了自己的需求,嘿嘿嘿。
首先是JSLINQ.js 代码。 下载地址
自己贴一下:
//-----------------------------------------------------------------------
// Part of the LINQ to JavaScript (JSLINQ) v2.20 Project - http://jslinq.codeplex.com
// Copyright (C) 2012 Chris Pietschmann (http://pietschsoft.com). All rights reserved.
// This license can be found here: http://jslinq.codeplex.com/license
//-----------------------------------------------------------------------
(function () {
var JSLINQ = window.jslinq = window.JSLINQ = function (dataItems) {
return new JSLINQ.fn.init(dataItems);
},
utils = {
processLambda: function (clause) {
// This piece of "handling" C#-style Lambda expression was borrowed from:
// linq.js - LINQ for JavaScript Library - http://lingjs.codeplex.com
// THANK!!
if (utils.isLambda(clause)) {
var expr = clause.match(/^[(\s]*([^()]*?)[)\s]*=>(.*)/);
return new Function(expr[1], "return (" + expr[2] + ")");
}
return clause;
},
isLambda: function (clause) {
return (clause.indexOf("=>") > -1);
},
randomIndex: function (max, existing) {
var q, r, f = function () { return this == r; };
if (!existing) {
return parseInt(Math.random() * max, 10);
} else {
q = JSLINQ(existing);
r = -1;
while (r < 0 || q.Where(f).Count() !== 0) {
r = utils.randomIndex(max);
}
return r;
}
}
};
JSLINQ.fn = JSLINQ.prototype = {
init: function (dataItems) {
this.items = dataItems;
}, // The current version of JSLINQ being used
jslinq: "2.20", toArray: function () { return this.items; },
where: function (clause) {
var newArray = [], len = this.items.length; // The clause was passed in as a Method that return a Boolean
for (var i = 0; i < len; i++) {
if (clause.apply(this.items[i], [this.items[i], i])) {
newArray[newArray.length] = this.items[i];
}
}
return JSLINQ(newArray);
},
select: function (clause) {
var item, newArray = [], field = clause;
if (typeof (clause) !== "function") {
if (clause.indexOf(",") === -1) {
clause = function () { return this[field]; };
} else {
clause = function () {
var i, fields = field.split(","), obj = {};
for (i = 0; i < fields.length; i++) {
obj[fields[i]] = this[fields[i]];
}
return obj;
};
}
} // The clause was passed in as a Method that returns a Value
for (var i = 0; i < this.items.length; i++) {
item = clause.apply(this.items[i], [this.items[i]]);
if (item) {
newArray[newArray.length] = item;
}
}
return JSLINQ(newArray);
},
orderBy: function (clause) {
var tempArray = [];
for (var i = 0; i < this.items.length; i++) {
tempArray[tempArray.length] = this.items[i];
} if (typeof (clause) !== "function") {
var field = clause;
if (utils.isLambda(field)) {
clause = utils.processLambda(field);
}
else {
clause = function () { return this[field]; };
}
} return JSLINQ(
tempArray.sort(function (a, b) {
var x = clause.apply(a, [a]), y = clause.apply(b, [b]);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
})
);
},
orderByDescending: function (clause) {
var tempArray = [], field;
for (var i = 0; i < this.items.length; i++) {
tempArray[tempArray.length] = this.items[i];
} if (typeof (clause) !== "function") {
field = clause;
if (utils.isLambda(field)) {
clause = utils.processLambda(field);
}
else {
clause = function () { return this[field]; };
}
} return JSLINQ(tempArray.sort(function (a, b) {
var x = clause.apply(b, [b]), y = clause.apply(a, [a]);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}));
},
selectMany: function (clause) {
var r = [];
for (var i = 0; i < this.items.length; i++) {
r = r.concat(clause.apply(this.items[i], [this.items[i]]));
}
return JSLINQ(r);
},
count: function (clause) {
if (clause === undefined) {
return this.items.length;
} else {
return this.Where(clause).items.length;
}
},
distinct: function (clause) {
var item, dict = {}, retVal = [];
for (var i = 0; i < this.items.length; i++) {
item = clause.apply(this.items[i], [this.items[i]]);
// TODO - This doesn't correctly compare Objects. Need to fix this
if (dict[item] === undefined) {
dict[item] = true;
retVal.push(item);
}
}
dict = null;
return JSLINQ(retVal);
},
any: function (clause) {
for (var i = 0; i < this.items.length; i++) {
if (clause.apply(this.items[i], [this.items[i], i])) { return true; }
}
return false;
},
all: function (clause) {
for (var i = 0; i < this.items.length; i++) {
if (!clause(this.items[i], i)) { return false; }
}
return true;
},
reverse: function () {
var retVal = [];
for (var i = this.items.length - 1; i > -1; i--) {
retVal[retVal.length] = this.items[i];
}
return JSLINQ(retVal);
},
first: function (clause) {
if (clause !== undefined) {
return this.Where(clause).First();
}
else {
// If no clause was specified, then return the First element in the Array
if (this.items.length > 0) {
return this.items[0];
} else {
return null;
}
}
},
last: function (clause) {
if (clause !== undefined) {
return this.Where(clause).Last();
}
else {
// If no clause was specified, then return the First element in the Array
if (this.items.length > 0) {
return this.items[this.items.length - 1];
} else {
return null;
}
}
},
elementAt: function (i) {
return this.items[i];
},
concat: function (array) {
var arr = array.items || array;
return JSLINQ(this.items.concat(arr));
},
intersect: function (secondArray, clause) {
var clauseMethod, sa = (secondArray.items || secondArray), result = [];
if (clause !== undefined) {
clauseMethod = clause;
} else {
clauseMethod = function (item, index, item2, index2) { return item === item2; };
} for (var a = 0; a < this.items.length; a++) {
for (var b = 0; b < sa.length; b++) {
if (clauseMethod(this.items[a], a, sa[b], b)) {
result[result.length] = this.items[a];
}
}
}
return JSLINQ(result);
},
defaultIfEmpty: function (defaultValue) {
if (this.items.length === 0) {
return defaultValue;
}
return this;
},
elementAtOrDefault: function (i, defaultValue) {
if (i >= 0 && i < this.items.length) {
return this.items[i];
}
return defaultValue;
},
firstOrDefault: function (defaultValue) {
return this.First() || defaultValue;
},
lastOrDefault: function (defaultValue) {
return this.Last() || defaultValue;
},
take: function (count) {
return this.Where(function (item, index) { return index < count; });
},
skip: function (count) {
return this.Where(function (item, index) { return index >= count; });
},
each: function (clause) {
var len = this.items.length;
for (var i = 0; i < len; i++) {
clause.apply(this.items[i], [this.items[i], i]);
}
return this;
},
random: function (count) {
var len = this.Count(), rnd = [];
if (!count) { count = 1; }
for (var i = 0; i < count; i++) {
rnd.push(utils.randomIndex(len - 1, rnd));
}
rnd = JSLINQ(rnd);
return this.Where(function (item, index) {
return rnd.Where(function () {
return this == index;
}).Count() > 0;
});
}
}; (function (fn) {
fn.ToArray = fn.toArray;
fn.Where = fn.where;
fn.Select = fn.select;
fn.OrderBy = fn.orderBy;
fn.OrderByDescending = fn.orderByDescending;
fn.SelectMany = fn.selectMany;
fn.Count = fn.count;
fn.Distinct = fn.distinct;
fn.Any = fn.any;
fn.All = fn.all;
fn.Reverse = fn.reverse;
fn.First = fn.first;
fn.Last = fn.last;
fn.ElementAt = fn.elementAt;
fn.Concat = fn.concat;
fn.Intersect = fn.intersect;
fn.DefaultIfEmpty = fn.defaultIfEmpty;
fn.ElementAtOrDefault = fn.elementAtOrDefault;
fn.FirstOrDefault = fn.firstOrDefault;
fn.LastOrDefault = fn.lastOrDefault;
fn.Take = fn.take;
fn.Skip = fn.skip;
fn.Each = fn.each;
fn.Random = fn.random;
})(JSLINQ.fn); JSLINQ.fn.init.prototype = JSLINQ.fn;
})();
使用方式:
页面代码
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LinqToJS</title>
<script src="~/Scripts/JSLINQ.js"></script>
<script src="~/Scripts/jquery-1.6.2.min.js"></script>
</head>
<body>
<div>
<h3>linq to js</h3>
</div>
</body>
</html>
<script type="text/javascript">
$(function() {
var str = "{\"zhang\":[{\"name\":\"张大佛爷\",\"Age\":\"170\",\"sex\":\"男\"},{\"name\":\"张启灵\",\"Age\":\"100\",\"sex\":\"男\"}],\"wu\":[{\"name\":\"吴邪\",\"Age\":\"25\",\"sex\":\"男\"}]}";
var list = JSON.parse(str); var sample1 = JSLINQ(list.zhang).Where(function () { return this.name == '张大佛爷'; }); var sample2 = JSLINQ(list.zhang).Select(function () { return this.name; }); return sample1; })
</script>
效果:就跟LINQ一样,没啥好说的,看一下方法格式就行
sample1 sample2


LIINQ TO JS的更多相关文章
- Vue.js 和 MVVM 小细节
MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...
- js学习笔记:操作iframe
iframe可以说是比较老得话题了,而且网上也基本上在说少用iframe,其原因大致为:堵塞页面加载.安全问题.兼容性问题.搜索引擎抓取不到等等,不过相对于这些缺点,iframe的优点更牛,跨域请求. ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- JS调用Android、Ios原生控件
在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...
- jquery和Js的区别和基础操作
jqery的语法和js的语法一样,算是把js升级了一下,这两种语法可以一起使用,只不过是用jqery更加方便 一个页面想要使用jqery的话,先要引入一下jqery包,jqery包从网上下一个就可以, ...
- 利用snowfall.jquery.js实现爱心满屏飞
小颖在上一篇一步一步教你用CSS画爱心中已经分享一种画爱心的方法,这次再分享一种方法用css画爱心,并利用snowfall.jquery.js实现爱心满屏飞的效果. 第一步: 利用伪元素before和 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- JS正则表达式常用总结
正则表达式的创建 JS正则表达式的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\\s+) ...
- 干货分享:让你分分钟学会 JS 闭包
闭包,是 Javascript 比较重要的一个概念,对于初学者来讲,闭包是一个特别抽象的概念,特别是ECMA规范给的定义,如果没有实战经验,很难从定义去理解它.因此,本文不会对闭包的概念进行大篇幅描述 ...
随机推荐
- 用CSS编写多种常见的图形
用CSS编写多种常见的图形 正方形与长方形 这个是最简单的,直接上代码 <!DOCTYPE html> <html> <head> <title>< ...
- zoj 1483 划分类DP
还是看了little_w大神写的才知道怎么写,看完发现自己题意也理解错了,里面有个neighboring,意思就是你指定任务的时候指定的是原序列中连续的一段 然后就是怎么DP了,新学了个很好的dp模型 ...
- metaspace 元空间
为何移除持久代 它的大小是在启动时固定好的, 很难进行调优 -XX:MaxPermSize(默认64M) HotSpot 的内部类型也是Java对象: 它可能会在Full GC中被移动, 同时它对应用 ...
- tcp协议与dup协议知识总结
在工作之余用xmind总结了一些UDP协议与TCP协议的知识点,如果有需要可以通过下方的留言,分享xmind文件和xmind软件.
- Docker 网络详解及 pipework 源码解读与实践
转载自:https://www.infoq.cn/article/docker-network-and-pipework-open-source-explanation-practice/ Docke ...
- JS正则和点击劫持代码(第十二天 9.27)
JS正则 正则表达式:用单个字符串描述或者匹配符合特定语句规则的字符串一些字符序列组合在一起,可以简单也可以复杂模式的,可以去搜索,可以去替换 语法:/表达式/修饰符(可选)var para=/icq ...
- Spring入门之一-------实现一个简单的IoC
一.场景模拟 public interface Human { public void goHome(); } Human:人类,下班了该回家啦 public interface Car { void ...
- 小程序实现倒计时:解决ios倒计时失效(setInterval失效)
在使用之前需要先在page页引入wxTimer.js文件(这里我将文件放在/utils) let timer = require('../../utils/wxTimer.js'); 然后就可以使用啦 ...
- 【shell】常用shell脚本
1.检查主机存活状态 #!/bin/bash IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2" for IP in $IP_LIST; ...
- SQL的7种连接查询详细实例讲解
SQL的7种连接查询详细实例讲解 原文链接:https://mp.weixin.qq.com/s/LZ6BoDhorW4cSBhaGy8VUQ 在使用数据库查询语句时,单表的查询有时候不能满足项目的业 ...