Javascript Equivalent to C# LINQ Select

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>;

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.

Yes, or $.map() does the same thing.

var ids ={
    return v.Id;

var ids2 = $.map(this.fruits, function (v){
    return v.Id;

console.log(ids, ids2);

Since isn't supported in older browsers, I suggest that you stick with the jQuery method.

If you prefer the other one for some reason you could always add a polyfill for old browser support.

You can always add custom methods to the array prototype as well: = function(expr){
    var arr = this;
    //do custom stuff
    return; //or $.map(expr);

var ids ={
    return v.Id;

An extended version that uses the function constructor if you pass a string. Something to play around with perhaps: = function(expr){
    var arr = this;

    switch(typeof expr){

        case 'function':
            return $.map(arr, expr);

        case 'string':


                var func = new Function(expr.split('.')[0], 
                                       'return ' + expr + ';');
                return $.map(arr, func);


                return null;


            throw new ReferenceError('expr not defined or not supported');




Since this has become such a popular answer, I'm adding similar my where() + firstOrDefault(). These could also be used with the string based function constructor approach (which is the fastest), but here is another approach using an object literal as filter:

Array.prototype.where = function (filter) {

    var collection = this;

    switch(typeof filter) { 

        case 'function': 
            return $.grep(collection, filter); 

        case 'object':
            for(var property in filter) {
                  continue; // ignore inherited properties

              collection = $.grep(collection, function (item) {
                  return item[property] === filter[property];
            return collection.slice(0); // copy the array 
                                      // (in case of empty object filter)

            throw new TypeError('func must be either a' +
                'function or an object of properties and values to filter by'); 

Array.prototype.firstOrDefault = function(func){
    return this.where(func)[0] || null;


var persons = [{ name: 'foo', age: 1 }, { name: 'bar', age: 2 }];

// returns an array with one element:
var result1 = persons.where({ age: 1, name: 'foo' });

// returns the first matching item in the array, or null if no match
var result2 = persons.firstOrDefault({ age: 1, name: 'foo' }); 

Here is a jsperf test to compare the function constructor vs object literal speed. If you decide to use the former, keep in mind to quote strings correctly.

My personal preference is to use the object literal based solutions when filtering 1-2 properties, and pass a callback function for more complex filtering.

I'll end this with 2 general tips when adding methods to native object prototypes:

  1. Check for occurrence of existing methods before overwriting e.g.:

    if(!Array.prototype.where) { Array.prototype.where = ...

  2. If you don't need to support IE8 and below, define the methods using Object.defineProperty to make them non-enumerable. If someone used on an array (which is wrong in the first place) they will iterate enumerable properties as well. Just a heads up.

I know it is a late answer but it was useful to me! Just to complete, using the $.grep function you can emulate the linq where().


var maleNames = people
.Where(p => p.Sex == "M")
.Select(p => p.Name)


// replace where  with $.grep
//         select with $.map
var maleNames = $.grep(people, function (p) { return p.Sex == 'M'; })
            .map(function (p) { return p.Name; });

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:

arrayFilter() -> Where()
arrayFirst() -> First()
arrayForEach() -> (no direct equivalent)
arrayGetDistictValues() -> Distinct()
arrayIndexOf() -> IndexOf()
arrayMap() -> Select()
arrayPushAll() -> (no direct equivalent)
arrayRemoveItem() -> (no direct equivalent)
compareArrays() -> (no direct equivalent)

So what you could do in your example is this:

var mapped = ko.utils.arrayMap(selectedFruits, function (fruit) {

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.

var mapped = Enumerable.From(selectedFruits)
    .Select("$.id") // 1 of 3 different ways to specify a selector function

The ES6 way:

let people = [{firstName:'Alice',lastName:'Cooper'},{firstName:'Bob',age:'Dylan'}];
let names = Array.from(people, p => p.firstName);
for (let name of names) {

also at:

You can also try linq.js

In linq.js your>;

will be

Enumerable.From(selectedFruits).Select(function (fruit) { return;  });

I have build a Linq library for TypeScript under 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.

Take a peek at underscore.js which provides many linq like functions. In the example you give you would use the map function.

You can try manipula package, which implement all of C# LINQ methods and save its syntax:

Your example>; will be implemented with manipula as


Dinqyjs has a linq-like syntax and provides polyfills for functions like map and indexOf, and has been designed specifically for working with arrays in Javascript.

I am answering the title of the question rather than the original question which was more specific.

With the new features of Javascript like iterators and generator functions and objects, something like LINQ for Javascript becomes possible. Note that linq.js, for example, uses a completely different approach, using regular expressions, probably to overcome the lack of support in the language at the time.

With that being said, I've written a LINQ library for Javascript and you can find it at Comments and discussion at

From previous answers, only Manipula seems to be what one would expect from a LINQ port in Javascript.

Take a look at fluent, it supports almost everything LINQ does and based on iterables - so it works with maps, generator functions, arrays, everything iterable.

The most similar C# Select analogue would be a map function. Just use:

var ids = =>;

to select all ids from selectedFruits array.

It doesn't require any external dependencies, just pure JavaScript. You can find map documentation here: