Following this question here :
Using the checked binding in knockout with a list of checkboxes checks all the checkboxes
I've created some checkboxes using knockout that allow selection from an array. working fiddle taken from above post:
Is there a simple way of creating an array of just the fruit's ID's?
I'm more at home with C# where I would do something along the lines of selectedFruits.select(fruit=>fruit.id);
Is there some method/ready made function for doing something similar with javascript/jquery? Or would the simplest option be to loop through the list and create a second array? I intend to post the array back to the server in JSON so am trying to minimize the data sent.
Take a peek at underscore.js which provides many linq like functions. In the example you give you would use the map function.
I have build a Linq library for TypeScript under TsLinq.codeplex.com that you can use for plain javascript too. That library is 2-3 times faster than Linq.js and contains unit tests for all Linq methods. Maybe you could review that one.
I know it is a late answer but it was useful to me! Just to complete, using the
$.grep
function you can emulate the linqwhere()
.Linq:
Javascript:
Since you're using knockout, you should consider using the knockout utility function
arrayMap()
and it's other array utility functions.Here's a listing of array utility functions and their equivalent LINQ methods:
So what you could do in your example is this:
If you want a LINQ like interface in javascript, you could use a library such as linq.js which offers a nice interface to many of the LINQ methods.
The ES6 way:
also at: https://jsfiddle.net/52dpucey/
You can also try
linq.js
In
linq.js
yourwill be