views:

83

answers:

2

Hi there,

i am looking for some input for how to use linq with javascript, there are number of implementations for javascript around.

anybody use any and which is the most compatible?

I was looking to be able to use linq against JSON and the DOM

Thanks in advance

+2  A: 

A LINQ-like library for JavaScript is JSLINQ. It seems to have quite a full implementation.

Example from site:

var myList = [
        {FirstName:"Chris",LastName:"Pearson"},
        {FirstName:"Kate",LastName:"Johnson"},
        {FirstName:"Josh",LastName:"Sutherland"},
        {FirstName:"John",LastName:"Ronald"},
        {FirstName:"Steve",LastName:"Pinkerton"}
        ];

var exampleArray = JSLINQ(myList)
                   .Where(function(item){ return item.FirstName == "Chris"; })
                   .OrderBy(function(item) { return item.FirstName; })
                   .Select(function(item){ return item.FirstName; });

This should be fine for JSON - as JSON is basically just objects. The DOM, while workable, might be a bit more clunky; you'd probably be best using something else.

Lucas Jones
JSON is a data format. What you get when you parse JSON is ordinary JavaScript object(s). It makes more sense to talk of JSON as being basically a string.
Tim Down
I am aware of that - I was talking about manipulating the parsed JSON and then reserialising it, which seems the most natural way to me.
Lucas Jones
Thanks for the link, i found another here http://linqjs.codeplex.com/ it seems a little more complete with delegate support, anybody used it?
mark smith
I've not used it before, but it seems just as good. :) I'm a bit confused about your statement regarding "complete with delegate support" - the first example on the site is C#, then the JS uses anonymous functions. Am I missing something?
Lucas Jones
Hi, yes sorry i wasn't clear... it mimics a delegate which in effect is an anonymous function /method....
mark smith
A: 

JavaScript libraries such as jQuery have methods that work on enumerables and provide filtering, projecting, etc like LINQ does. For example, the jQuery.grep() method works just like LINQ's Where() by filtering items according to the given (anonymous) function and jQuery.map() projects items like LINQ's Select().

Lucas