JavaScript ES6 Linq

- JavaScript Developer and Blogger
Published: Sat May 09 2015

EcmaScript 6 offers some new really nice syntax additions to JavaScript. One of them is arrow functions. Arrow functions provide a compact way to create filter functions, so to take advantage of this I have created a lightweight library for querying arrays using ES6 arrow functions.

The API is very similar to the syntax used by lambda based Linq functions in C#


let people = [{firstName:'Joe',age:33},{firstName:'Jane',age:20},{firstName:'Peter',age:44}]; let res = $select.from(people).where(p => p.age > 40).toArray(); let res = $select.from(people).where(p => p.age > 10).take(2);
The selector takes an array as the only argument and executes your defined query function against it. Calling toArray() returns all matching results, but you can limit the result further by calling take(n) instead.


var person = $select.from(people).first(p => p.age === 44);
first() will return the first matching result.


let items = [{name:'Item1',price:122},{name:'Item2',price:122},{name:'Item3',price:155}] let all = $select.from(items).all(i => i.price > 10); let any = $select.from(items).any(i => i.price === 155);
all() returns true if all items match the criteria whereas any will return true if any of the items match. False is returned for both if no match is found.

The library is open source and can be downloaded here: GitHub. The project is already configured with ES6 transpiling using Babel triggered by npm

There are two npm tasks defined

npm run tests //transpiles and runs ES6 unit tests npm run sample //transpiles sample project code

I invite you to follow me on twitter