Click here to Skip to main content
12,633,764 members (31,059 online)
Click here to Skip to main content
Add your own
alternative version

Stats

10.8K views
15 bookmarked
Posted

LINQ2TypeScript

, 20 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Some ideas to bring LINQ goodness to JavaScript array

Introduction

This tip gives some ideas to bring some of the C# LINQ goodness to TypeScript (and, by extension, JavaScript).

Using the Code

While starting to get seriously into client side and JavaScript heavy application, I started to seriously miss the concise way LINQ was simplifying working with collection.

Not to worry, we can do it in JavaScript too! Even better, we can do it in a strongly typed way in TypeScript!

Here I intend to show how to extend JavaScript array with .where() and .select() !

// wouldn't it be nice to write
var names = item.where(x => x.ID > 5).select(x => x.Name)  

Well, why not? Here is the TypeScript definition of those methods!

interface Array<T> {
    select<U>(cvt: (item: T) => U): Array<U>;
    where(predicate: (item: T) => boolean): Array<T>;
}
/** return a subset of an array matching a predicate */
Array.prototype.where = function <T>(predicate: (item: T) => boolean): Array<T> {
    var result = [];
    for (var i = 0; i < this.length; i++) {
        var item = this[i];
        if (predicate(item))
            result.push(item);
    }
    return result;
};
/** convert an array to another one */
Array.prototype.select = function <T, U>(cvt: (item: T) => U): Array<U> {
    var result = [];
    for (var i = 0; i < this.length; i++) {
        var item = this[i];
        var ci = cvt(item);
        result.push(ci);
    }
    return result;
};

History

This is the first draft.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Super Lloyd
Software Developer (Senior) http://www.ansibleww.com.au
Australia Australia
The Australia born French man who went back to Australia later in life...
Finally got over life long (and mostly hopeless usually, yay!) chronic sicknesses.
Worked in Sydney, Brisbane, Darwin, Billinudgel, Darwin and Melbourne.

You may also be interested in...

Comments and Discussions

 
QuestionCheck out IXJS Pin
David Hanson24-Mar-14 12:11
memberDavid Hanson24-Mar-14 12:11 
AnswerRe: Check out IXJS Pin
Super Lloyd24-Mar-14 14:01
memberSuper Lloyd24-Mar-14 14:01 
QuestionNice Pin
Sacha Barber21-Mar-14 2:50
mvpSacha Barber21-Mar-14 2:50 
AnswerRe: Nice Pin
Super Lloyd21-Mar-14 21:11
memberSuper Lloyd21-Mar-14 21:11 
GeneralSuper! Pin
Meshack Musundi20-Mar-14 23:59
professionalMeshack Musundi20-Mar-14 23:59 
GeneralRe: Super! Pin
Super Lloyd21-Mar-14 21:13
memberSuper Lloyd21-Mar-14 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 21 Mar 2014
Article Copyright 2014 by Super Lloyd
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid