Linq.js CRUD Methods

One of my favorite JavaScript libraries is Linq.js (LINQ for JavaScript). Unfortunately the author is not responding to pull requests so I’m going to post my update here.

I’ve added CRUD methods to the library so that you can Add, Delete, and Update items easily. I’ve found this very useful pattern where I load a lot of data when the page loads, convert it to an Enumerable using Linq.js, then as the user modifies data update it locally and send updates back to the server asynchronously.

Here are examples of each method I’ve added:

var data = Enumerable.From([ { "id": 1, "url": "", "title": "Amazon" }, { "id": 2, "url": "", "title" : "Google" }, { "id": 3, "url": "", "title" : "Wrong" } ]); data.Add({ "id": 4, "url": "", "title" : "MSN" }); data.Delete(function(x) { return == 1; }); data.Update(function(x) { return == 3 }, { "id": 3, "url": "", "title" : "Yahoo!" });

You can download my linq.js file here. You can also play with a fiddle I created using the example above.

Alternatively you can pass a function to Update instead of an object (makes sense if you are updating more than one item at a time):

data.Update(function(x) { return x; }, function(x) { x.title = "updated " +; return x; });

Tags: linq

Rush Frisby

Rush is a software architect who loves solving problems. You'll usually find him writing about security concepts and sharing solutions to problems he's faced.