LINQ in Action - LINQ Book & News

LINQ to Objects for JavaScript

Nikhil Kothari, whose blog you should visit immediately if you're doing web development, demonstrates how to reproduce LINQ to Objects in JavaScript.

Nikhil is an architect on the Web Platform and Tools team at Microsoft known for his great projects, past and present, which include: ASP.NET Web Matrix, Web Development Helper, and Script#.

This time, Nikhil has a post in which he demonstrates how it's possible to write queries against JavaScript arrays, similarly to what LINQ to Objects offers for C# and VB.NET.

The following LINQ query in C#:

var someLocations =
  from location in allLocations
  where location.City.Length > 6
  select new { City = location.City, Country = location.Country };

can also be written as follows using query operators:

var someLocations =
 
allLocations
    .Where(
location => location.City.Length > 6)
    .Select(location => new { City = location.City, Country = location.Country });

The equivalent query could be written in JavaScript as follows: 

var someLocations =
  allLocations
    .filter(function(location) { return location.City.length > 6; })
    .map(function(location) { return { City: location.City, Country: location.Country }; });

Pretty close, isn't it? Learn more...

Published Thursday, December 28, 2006 12:41 PM by Fabrice Marguerie

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

Featured Item

This Blog

Sponsoring

Syndication

Tags

No tags have been created or used yet.