Giter Site home page Giter Site logo

typescript's Introduction

TypeScript common linq command equivalents / CheatSheet

πŸ“Œ Intro

Linq in c# is a great abstraction, it massively reduces the amount of code to do fairly basic operations, using TypeScript doesn’t mean you lose this functionality, its just a little different to achieve many of the operations. In this article I’ll run through the basic Linq-To-Object operations, and how you can achieve similar results in TypeScript.

For these examples, I’ll keep it simple with a list of Person objects that look like this:

public class Person
{
    public string Name {get;set;}
    public string Title {get;set;}
}
class Person
{
    constructor(public Name: string, public Title: string)
    {        
    }
}

(How good is that typescript constructor/field syntax!).

With the data:

  • Chandler - Mr
  • Monica - Mrs
  • Rachel - Miss
  • Joey - Mr
  • Ross - Dr

πŸ“Œ Select

To select the names of each person with their title:

CSharp
people.Select(x => new { FullTitle = $"{x.Title} {x.Name}"});
Typescript

In TypeScript, the equivalent to select is the map function:

people.map(x => ({ FullTitle: x.Title + ' ' + x.Name }));

πŸ“Œ Where

To filter the list to only the people with the title β€œMr”:

CSharp
people.Where(x => x.Title == "Mr");
Typescript
people.filter(x => x.Title == "Mr");

πŸ“Œ OrderBy

To order the list by Name:

CSharp
people.OrderBy(x => x.Name);
Typescript

In TypeScript, the equivalent to order by is the sort function, but you do have to give it a sort function that returns 1 or 0:

people.sort((x,y) => x.Name > y.Name ? 1 : -1);

πŸ“Œ GroupBy

To group the list into the various titles:

CSharp
people.GroupBy(x => x.Title);
Typescript

There isn’t a simple equivalent to this, the closest thing is probably the reduce function:

var grouped = people.reduce((g : any, person : Person) => {
    g[person.Title] = g[person.Title] || []; //Check the value exists, if not assign a new array
    g[person.Title].push(person); //Push the new value to the array
    return g; //Very important! you need to return the value of g or it will become undefined on the next pass
}, {});

πŸ“Œ FirstOrDefault

To select the first person with the title Mr, or null if none exist:

CSharp
people.FirstOrDefault(x => x.Title == "Mr");
Typescript
people.find(x => x.Title == "Mr");

πŸ“Œ Aggregate

Lets concatenate all the names together:

CSharp
people.Select(x => x.Name).Aggregate((x,y) => x = x + "" + y).Dump();
Typescript

This time, reduce is almost like for like with the c# equivalent

var concat = people.map(x => x.Name).reduce((g : any, name: string) => {
    g += name;
    return g;
}, "");

typescript's People

Contributors

srinivas-ramu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.