JSPM

  • Created
  • Published
  • Downloads 39
  • Score
    100M100P100Q69509F
  • License MIT

use linq and lamdba in javascript

Package Exports

  • linq-js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (linq-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

LinqJs

GitHub release npm npm Travis branch

use linq and lambda in javascript 在javascript中使用linq与lambda

在1.0.0中使用了字符串的lambda表达式,过于繁琐,并且不支持延迟操作 Since 2.1.0, I rewrite all to use new features of ES6. The performance be better, memory is used less and using deferred execution. 从2.1.0开始整体代码重新编写,使用全新的ES6的特性,性能更好,同时对数据的操作是延时操作,占用更少

帮助文档(完善中) 👍 Documentation(not completed) 👍

Usage 用法

1. Import 引入

Use NodeJs 使用NodeJs

$ npm install --save linq-js
const Enumerable = require('linq-js');
  • description:This module require ES6. I suggest you to use this with ES6. The following examples is already use ES6.
  • 说明:本module依赖于ES6。建议项目在中使用ES6。以下案例中将均使用ES6写法。

2. Get IEnumerable instance 获取IEnumerable对象

interface IEnumerable { };

function asEnumerable():IEnumerable;
//You can use the asEnumerable methods of every object's to get an IEnumerable object;
//任何对象都有asEnumerable方法用来获取IEnumerable对象

e.g. 案例

'abc'.asEnumerable();
[1,2,3].asEnumerable();
({a:1,b:2}).asEnumerable();

3. Use IEnumerable instance 使用IEnumerable对象

e.g. 案例

let pets = [ { name: "Barley", age: 8, vaccinated: true }, { name: "Boots", age: 4, vaccinated: false }, { name: "Whiskers", age: 1, vaccinated: false } ];
let unvaccinated = pets.asEnumerable().any(p => p.age > 1 && p.vaccinated === false);
console.log(`There ${ unvaccinated ? "are" : "are not any" } unvaccinated animals over age one.`);
// This code produces the following output: 这段代码输出以下内容:
//  There are unvaccinated animals over age one.

e.g. 案例

let magnus = { name: "Hedlund, Magnus" }, terry = { name: "Adams, Terry" }, charlotte = { name: "Weiss, Charlotte" };
let barley = { name: "Barley", owner: terry }, boots = { name: "Boots", owner: terry }, whiskers = { name: "Whiskers", owner: charlotte }, daisy = { name: "Daisy", owner: magnus };
let people = [ magnus, terry, charlotte ];
let pets = [ barley, boots, whiskers, daisy ];
let query = people.asEnumerable().join(pets,
    (person, pet) => ({ ownerName: person.name, pet: pet.name }),
    person => person,
    pet => pet.owner);
for (let obj of query) {
    console.log(`${ obj.ownerName } - ${ obj.pet }`);
}
/*
 This code produces the following output: 这段代码输出以下内容:
 Hedlund, Magnus - Daisy
 Adams, Terry - Barley
 Adams, Terry - Boots
 Weiss, Charlotte - Whiskers
 */

:see msdn(IEnumerable) :参考 MSDN(IEnumerable)
:see msdn(Enumerable) :参考 MSDN(Enumerable)