Wednesday, September 17, 2014

Linq in JavaScript?

Do you miss Linq in JavaScript? qArr.js offer some basic Linq query features when working with arrays in JavaScript.

Library: https://github.com/waltersoto/qArr.js 

To illustrate how qArr.js works we'll do some examples:


First, we need a dataset to query against; so, let's create an array with information about the top 10 box office movies of all time.

var allTimeBoxOffice = [
        { rank: 1, title: 'Avatar', studio: 'Fox', grossInB: 2788, year: 2009 },
        { rank: 2, title: 'Titanic', studio: 'Paramount', grossInB: 2186.8, year: 1997 },
        { rank: 3, title: 'Marvel\'s The Avengers', studio: 'Buena Vista', grossInB: 1518.6,           year: 2012 },
        { rank: 4, title: 'Harry Potter and the Deathly Hallows Part 2'
          studio: 'Warner Bros.', grossInB: 1341.5, year: 2011 },
        { rank: 5, title: 'Frozen', studio: 'Buena Vista', grossInB: 1274.2, year: 2013 },
        { rank: 6, title: 'Iron Man 3', studio: 'Buena Vista', grossInB: 1215.4, 
          year: 2011 },
        { rank: 7, title: 'Transformers: Dark of the Moon', studio: 'Paramount',
          grossInB: 1123.8, year: 2011 },
        { rank: 8, title: 'The Lord of the Rings: The Return of the King'
          studio: 'New Line', grossInB: 1119.9, year: 2003 },
        { rank: 9, title: 'Skyfall', studio: 'Sony', grossInB: 1108.6, year: 2012 },
        { rank: 10, title: 'The Dark Knight Rises', studio: 'Warner Bros.'
          grossInB: 1084.4, year: 2012 }


Querying movies from 2011 using .where().

var from2011 = qA(allTimeBoxOffice).where(function (x) { return x.year === 2011; }).toArray();


The example above will generate the following dataset:

[
 {"rank":4,"title":"Harry Potter and the Deathly Hallows Part 2","studio":"Warner Bros.","grossInB":1341.5,"year":2011},
 {"rank":6,"title":"Iron Man 3","studio":"Buena Vista","grossInB":1215.4,"year":2011},
 {"rank":7,"title":"Transformers: Dark of the Moon","studio":"Paramount","grossInB":1123.8,"year":2011}
]

Now, lets select a list of movie titles sorted by year in descending order by using .select() and .orderByDescending():

var titles = qA(allTimeBoxOffice).select(function (x) { return x.title; })

                     .orderByDescending(function (x) { return x.year; }).toArray();

The example above will generate the following dataset:

["Transformers: Dark of the Moon","Titanic","The Lord of the Rings: The Return of the King","The Dark Knight Rises","Skyfall","Marvel's The Avengers","Iron Man 3","Harry Potter and the Deathly Hallows Part 2","Frozen","Avatar"]

As a last example, lets group and sort the dataset using year as a category with .groupBy() and .orderBy().

   var groupedByYear = qA(allTimeBoxOffice).groupBy(function (x) { return x.year; })
                                                .orderBy(function (x) { return x.key; })
                                                .toArray();

The example above will generate the following dataset:
[
  {"key":1997,"item":[{"rank":2,"title":"Titanic","studio":"Paramount","grossInB":2186.8,"year":1997}]},

  {"key":2003,"item":[{"rank":8,"title":"The Lord of the Rings: The Return of the King","studio":"New Line","grossInB":1119.9,"year":2003}]},
  
  {"key":2009,"item":[{"rank":1,"title":"Avatar","studio":"Fox","grossInB":2788,"year":2009}]},
  
  {"key":2011,"item":[{"rank":4,"title":"Harry Potter and the Deathly Hallows Part 2","studio":"Warner Bros.","grossInB":1341.5,"year":2011},{"rank":6,"title":"Iron Man 3","studio":"Buena Vista","grossInB":1215.4,"year":2011},{"rank":7,"title":"Transformers: Dark of the Moon","studio":"Paramount","grossInB":1123.8,"year":2011}]},

  {"key":2012,"item":[{"rank":3,"title":"Marvel's The Avengers","studio":"Buena Vista","grossInB":1518.6,"year":2012},{"rank":9,"title":"Skyfall","studio":"Sony","grossInB":1108.6,"year":2012},{"rank":10,"title":"The Dark Knight Rises","studio":"Warner Bros.","grossInB":1084.4,"year":2012}]},

  {"key":2013,"item":[{"rank":5,"title":"Frozen","studio":"Buena Vista","grossInB":1274.2,"year":2013}]}
]