1 Star 0 Fork 41

x-l-r/JsonSql.js

forked from @HuangBingGui/JsonSql.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
JsonSql_Sample.txt 3.88 KB
一键复制 编辑 原始数据 按行查看 历史
@HuangBingGui 提交于 2020-03-27 09:55 . update JsonSql_Sample.txt.
//https://gitee.com/JeeHuangBingGui/JsonSql
//对象用$.x ,数组用$[index]
//查询对象或Json或数组
var jsonArray = [
{ "user": { "id": 100, "screen_name": "d_linq" }, "text": "to objects" },
{ "user": { "id": 130, "screen_name": "c_bill" }, "text": "g" },
{ "user": { "id": 155, "screen_name": "b_mskk" }, "text": "kabushiki kaisha" },
{ "user": { "id": 301, "screen_name": "a_xbox" }, "text": "halo reach" }
]
var jsonArray = [
[100,"d_linq","to objects"],
[130,"c_bill","g"],
[155,"b_mskk","kabushiki kaisha"],
[301,"a_xbox","halo reach"]
]
// ["b_mskk:kabushiki kaisha", "c_bill:g", "d_linq:to objects"]
var queryResult = Enumerable.From(jsonArray)
.Where(function (x) { return x.user.id < 200 })
.OrderBy(function (x) { return x.user.screen_name })
.Select(function (x) { return x.user.screen_name + ':' + x.text })
.ToArray();
// shortcut! string lambda selector
var queryResult2 = Enumerable.From(jsonArray)
.Where("$.user.id < 200")
.OrderBy("$.user.screen_name")
.Select("$.user.screen_name + ':' + $.text")
.ToArray();
//数组
var queryResult2 = Enumerable.From(jsonArray)
.Where("$[0] < 200")
.OrderBy("$[1]")
.Select("$[1] + ':' + $[2]")
.ToArray();
var jsonArray = [
{ "StuID": 1, "StuName": "James", "Age": 30, "Country": "USA" },
{ "StuID": 2, "StuName": "Byant", "Age": 36, "Country": "USA" },
{ "StuID": 3, "StuName": "Lin", "Age": 30, "Country": "Taiwan" },
{ "StuID": 4, "StuName": "Yao", "Age": 30, "Country": "Shanghai" },
{ "StuID": 5, "StuName": "James", "Age": 30, "Country": "USA" },
{ "StuID": 6, "StuName": "Byant", "Age": 36, "Country": "USA" },
{ "StuID": 7, "StuName": "Lin", "Age": 30, "Country": "Taiwan" },
{ "StuID": 8, "StuName": "zhangsan", "Age": 30, "Country": "China" },
];
JsonSql.From(jsonArray)
.Where("$.Age <= 30")//筛选
.Distinct("$.StuName")//去重
.OrderBy("$.StuID")//排序
.Skip(0)//跳过0条
.Take(2)//取2条
.ToJSON()
//.ToString()
//.ToArray()
//列获取
JsonSql.From(jsonArray)
.Select("{StuName:$.StuName,age:$.Age}")
.ToJSON()
//合计
JsonSql.From(jsonArray)
.Sum("$.Age")
.ToJSON()
//Min
JsonSql.From(jsonArray)
.Where("$.Age = JsonSql.From(jsonArray).Min('$.Age')")
.ToJSON()
//数组转对象
JsonSql.From(gp)
.Select("{day:$[0],close:$[1]}")
.Where("$.close == JsonSql.From(gp).Min('$[1]')")
.ToJSON()
//like 包含
JsonSql.From(gp)
.Select("{day:$[0],close:$[2]}")
.Where("$.day.indexOf('2020-03-19') != -1")
.ToJSON()
//直接写sql语句
$JsonSql(" select {day:$[0],close:$[2]} from gp where $.close == JsonSql.From(gp).Min('$[2]')")
$JsonSql(" from gp where $[2] == JsonSql.From(gp).Min('$[2]')")
//ForEach
var jsonArray = [
{{ "StuID": 1, "StuName": "James", "Age": 30, "Country": "USA" },
{ "StuID": 2, "StuName": "Byant", "Age": 36, "Country": "USA" },
{ "StuID": 3, "StuName": "Lin", "Age": 30, "Country": "Taiwan" },
{ "StuID": 4, "StuName": "Yao", "Age": 30, "Country": "Shanghai" }
];}
var querResult = JsonSql.From(jsonArray)
.Where(function (x) { return x.Age <= 30; })
.OrderBy(function (x) { return x.StuID; })
.ToArray()
.ForEach(function (i) {
document.write(i.StuID + ";" + i.StuName + ";" + i.Age + ";" + i.Country + "<br/>");
});
//交集、差集、并集
var arr1 = [1, 412, 53, 25];
var arr2 = [53, 25,22,20];
Enumerable.From(arr1).Intersect(arr2).ForEach(function(i) {
document.write(i + ";"); //53,25
});
Enumerable.From(arr1).Except(arr2).ForEach(function (i) {
document.write(i + ";"); // 1;412
});
Enumerable.From(arr1).Union(arr2).ForEach(function (i) {
document.write(i + ";"); // 1;412;53;25;22;20;
});
//Alternate、Contains
Enumerable.Range(1, 5).Alternate('*').ForEach(function (i) {
document.write(i + ";"); //;*;2;*;3;*;4;*;
});
var r = Enumerable.Range(1, 5).Contains(3);
document.write(r); //ture
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/x-l-r/JsonSql.git
git@gitee.com:x-l-r/JsonSql.git
x-l-r
JsonSql
JsonSql.js
master

搜索帮助