Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Underscore.js - Overview of functions, Part I: Collections and Arrays

Sidnet
June 30, 2015

Underscore.js - Overview of functions, Part I: Collections and Arrays

Sidnet

June 30, 2015
Tweet

More Decks by Sidnet

Other Decks in Programming

Transcript

  1. Outline Part I: • Introduction • Collections • Arrays Part

    II: • Functions • Objects • Utilities • Summary
  2. What is Underscore.js? • A JavaScript library which provides utility

    functions for common programming tasks • Made by Jeremy Ashkenas (also known for Backbone.js and CoffeeScript) • Similar to features provided by Prototype.js and the Ruby language • Opts for a functional programming design instead of extending object prototypes
  3. What is Underscore.js? • Provides over 100 functions • Common

    workaday functional helpers – e.g. map, filter, invoke • But also more specialized goodies: function binding, javascript templating, creating quick indexes, deep equality testing, and so on.
  4. What is Underscore.js? • The documentation refers to Underscore.js as

    "the tie to go along with jQuery's tux, and Backbone.js's suspenders" • Currently Underscore.js is one of the most depended upon packages in the Node.js JavaScript runtime
  5. Dependencies • Underscore.js requires no dependencies and in fact it

    falls back to the native implementations when available • If the environment supports ECMA Script 5, implementations for forEach, map, reduce, keys, bind and even more, those implementations are used in favor of what's implemented in Underscore.js itself
  6. Getting Started • All you need to do it's add

    it to your project – download the source code, include it in your page and you're good to go • Can be installed via popular package managers like: Node.js, Meteor.js, Require.js, Bower or Component
  7. Getting Started • Underscore creates and exposes all its functionality

    via a single global scope object • This object is the titular underscore: _ • Similar to how jQuery works with the dollar ($) symbol • Like with jQuery, you can remap the object to avoid conflicts: _.noConflict()
  8. Functional or Object-Oriented? You can use Underscore in either an

    object- oriented or a functional style, depending on your preference. var object = { "key1": "value1", "key2": "value2" };
  9. Functional or Object-Oriented? You can use Underscore in either an

    object- oriented or a functional style, depending on your preference. var object = { "key1": "value1", "key2": "value2", }; _(object).keys(); // => ["key1", "key2"]
  10. Functional or Object-Oriented? You can use Underscore in either an

    object- oriented or a functional style, depending on your preference. var object = { "key1": "value1", "key2": "value2", }; _(object).keys(); // => ["key1", "key2"] _.keys(object); // => ["key1", "key2"]
  11. Collection Functions (Arrays or Objects) • Collection functions work on

    arrays, objects, and array-like objects (such as arguments and NodeList) • They use duck-typing, so passing objects with a numeric length property should be avoided
  12. Collection Functions (Arrays or Objects) contains invoke sample countBy map

    shuffle each max size every min some filter partition sortBy find pluck toArray findWhere reduce where groupBy reduceRight indexBy reject
  13. each (alias: forEach) Underscore.js docs: Iterates over a list of

    elements, yielding each in turn to an iteratee function
  14. each (alias: forEach) var obj = { "key1": "value1", ...

    }; $.each(obj, function(key, value) { key + ": " + value; }); // => key1: value1... Underscore.js: jQuery: _.each(obj, function(value, key) { key + ": " + value; }); // => key1: value1...
  15. each (alias: forEach) var arr = ["value1", "value2", "value3"]; $.each(arr,

    function(index, value) { index + ": " + value; }); // => 0: value1... Underscore.js: jQuery: _.each(arr, function(value, index) { index + ": " + value; }); // => 0: value1...
  16. each (alias: forEach) var arr = ["value1", "value2", "value3"]; $.each(arr,

    function(index, value) { this; }); // => ? Underscore.js: jQuery: _.each(arr, function(value, index) { this; }); // => ?
  17. each (alias: forEach) var arr = ["value1", "value2", "value3"]; $.each(arr,

    function(index, value) { this; }); // => "value1" Underscore.js: jQuery: _.each(arr, function(value, index) { this; }); // => Window
  18. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; $.each(arr, function(index, value) {

    if (value === "val2") return; if (value === "val4") return false; alert(value); }); jQuery:
  19. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; $.each(arr, function(index, value) {

    if (value === "val2") return; if (value === "val4") return false; alert(value); }); // => alerts "val1" and "val3" jQuery:
  20. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; $.each(arr, function(index, value) {

    if (value === "val2") return; // => "continue" if (value === "val4") return false; alert(value); }); // => alerts "val1" and "val3" jQuery:
  21. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; $.each(arr, function(index, value) {

    if (value === "val2") return; // => "continue" if (value === "val4") return false; // => "break" alert(value); }); // => alerts "val1" and "val3" jQuery:
  22. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; _.each(arr, function(value, index) {

    if (value === "val2") return; if (value === "val4") return false; alert(value); }); Underscore.js:
  23. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; _.each(arr, function(value, index) {

    if (value === "val2") return; if (value === "val4") return false; alert(value); }); // => alerts "val1", "val3", "val5" Underscore.js:
  24. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; _.each(arr, function(value, index) {

    if (value === "val2") return; // => "continue" if (value === "val4") return false; alert(value); }); // => alerts "val1", "val3", "val5" Underscore.js:
  25. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; _.each(arr, function(value, index) {

    if (value === "val2") return; // => "continue" if (value === "val4") return false; // => "continue" alert(value); }); // => alerts "val1", "val3", "val5" Underscore.js:
  26. each (alias: forEach) arr = ["val1","val2","val3",“val4”,“val5”]; _.each(arr, function(value, index) {

    if (value === "val2") return; // => "continue" if (value === "val4") return false; // => "continue" alert(value); }); // Can't break out of a _.each loop Underscore.js:
  27. each (alias: forEach) var arr = ["key1","key2","key3"]; var obj =

    {"key1": "val1", ... }; _.each(arr, function(value, index, list) { value; index; list; this; this[value]; }, obj);
  28. each (alias: forEach) var arr = ["key1","key2","key3"]; var obj =

    {"key1": "val1", ... }; _.each(arr, function(value, index, list) { value; // => "key1" index; list; this; this[value]; }, obj);
  29. each (alias: forEach) var arr = ["key1","key2","key3"]; var obj =

    {"key1": "val1", ... }; _.each(arr, function(value, index, list) { value; // => "key1" index; // => 0 list; this; this[value]; }, obj);
  30. each (alias: forEach) var arr = ["key1","key2","key3"]; var obj =

    {"key1": "val1", ... }; _.each(arr, function(value, index, list) { value; // => "key1" index; // => 0 list; // => arr this; this[value]; }, obj);
  31. each (alias: forEach) var arr = ["key1","key2","key3"]; var obj =

    {"key1": "val1", ... }; _.each(arr, function(value, index, list) { value; // => "key1" index; // => 0 list; // => arr this; // => obj this[value]; }, obj);
  32. each (alias: forEach) var arr = ["key1","key2","key3"]; var obj =

    {"key1": "val1", ... }; _.each(arr, function(value, index, list) { value; // => "key1" index; // => 0 list; // => arr this; // => obj this[value]; // => "value1" }, obj);
  33. map (alias: collect) Underscore.js docs: Produces a new array of

    values by mapping each value in list through a transformation function (iteratee).
  34. map (alias: collect) results = _.map([1, 2, 3], function (num)

    { return num * 3; }); results = _.map({one: 1, two: 2, three: 3}, function (num, key) { return num * 3; } );
  35. map (alias: collect) results = _.map([1, 2, 3], function (num)

    { return num * 3; }); // => [3, 6, 9] results = _.map({one: 1, two: 2, three: 3}, function (num, key) { return num * 3; } );
  36. map (alias: collect) results = _.map([1, 2, 3], function (num)

    { return num * 3; }); // => [3, 6, 9] results = _.map({one: 1, two: 2, three: 3}, function (num, key) { return num * 3; } ); // => [3, 6, 9]
  37. map (alias: collect) var params = { name: 'alice', loc:

    'US', category: 'clothing' }; _.map(params, function (value, key) { return key + '=' + value; }).join('&');
  38. map (alias: collect) var params = { name: 'alice', loc:

    'US', category: 'clothing' }; _.map(params, function (value, key) { return key + '=' + value; }).join('&'); // => name=alice&loc=US&category=clothing
  39. reduce (aliases: inject, foldl) Underscore.js docs: Boils down a list

    of values into a single value. Memo is the initial state of the reduction, and each successive step of it should be returned by iteratee. The iteratee is passed four arguments: the memo, then the value and index (or key) of the iteration, and finally a reference to the entire list. Right-associative version: reduceRight (foldr)
  40. reduce (aliases: inject, foldl) sum = _.reduce([1, 2, 3], function

    (memo, num) { return memo + num; }, 0); petsCount = _.reduce( pets, function (memo, pet) { if (pet.species === 'cat') memo.cats++; if (pet.species === 'dog') memo.dogs++; return memo; }, { cats: 0, dogs: 0 } );
  41. reduce (aliases: inject, foldl) sum = _.reduce([1, 2, 3], function

    (memo, num) { return memo + num; }, 0); // => 6 petsCount = _.reduce( pets, function (memo, pet) { if (pet.species === 'cat') memo.cats++; if (pet.species === 'dog') memo.dogs++; return memo; }, { cats: 0, dogs: 0 } );
  42. reduce (aliases: inject, foldl) sum = _.reduce([1, 2, 3], function

    (memo, num) { return memo + num; }, 0); // => 6 petsCount = _.reduce( pets, function (memo, pet) { if (pet.species === 'cat') memo.cats++; if (pet.species === 'dog') memo.dogs++; return memo; }, { cats: 0, dogs: 0 } ); // => { cats: 7, dogs: 5 }
  43. find (alias: detect) Underscore.js docs: Looks through each value in

    the list, returning the first one that passes a truth test (predicate), or undefined if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.
  44. find (alias: detect) even = _.find([1, 2, 3, 4], function

    (num) { return num % 2 == 0; }); var primaryAccount = _.find( accounts, function (account) { return account.type === "primary"; } );
  45. find (alias: detect) even = _.find([1, 2, 3, 4], function

    (num) { return num % 2 == 0; }); // => 2 var primaryAccount = _.find( accounts, function (account) { return account.type === "primary"; } );
  46. find (alias: detect) even = _.find([1, 2, 3, 4], function

    (num) { return num % 2 == 0; }); // => 2 var primaryAccount = _.find( accounts, function (account) { return account.type === "primary"; } ); // => { number: "...", type: "primary" }
  47. filter (alias: select) Underscore.js docs: Looks through each value in

    the list, returning an array of all the values that pass a truth test (predicate).
  48. filter (alias: select) var even = _.filter( [1, 2, 3,

    4, 5, 6], function (num) { return num % 2 == 0; } );
  49. filter (alias: select) var even = _.filter( [1, 2, 3,

    4, 5, 6], function (num) { return num % 2 == 0; } ); // => [2, 4, 6]
  50. filter (alias: select) var filtered = _.filter(json, function (item) {

    return (item.status === "active" && item.priority === "high"); });
  51. where Underscore.js docs: Looks through each value in the list,

    returning an array of all the values that contain all of the key-value pairs listed in properties.
  52. findWhere Underscore.js docs: Looks through the list and returns the

    first value that matches all of the key-value pairs listed in properties. If no match is found, or if list is empty, undefined will be returned.
  53. reject Underscore.js docs: Returns the values in list without the

    elements that the truth test (predicate) passes. The opposite of filter.
  54. reject var odd = _.reject( [1, 2, 3, 4, 5,

    6], function(num) { return num % 2 == 0; } );
  55. reject var odd = _.reject( [1, 2, 3, 4, 5,

    6], function(num) { return num % 2 == 0; } ); // => [1, 3, 5]
  56. reject var availableDomains = ["test.com", "test.net", "test.org"]; _.reject( availableDomains, function

    (element) { return element === "test.net"; } ); // => ["test.com", "test.org"]
  57. every (alias: all) Underscore.js docs: Returns true if all of

    the values in the list pass the predicate truth test.
  58. every (alias: all) _.every( [true, 1, null, 'yes'], function(value) {

    return value; } ); // => false _.every([true, 1, null, 'yes'], _.identity);
  59. every (alias: all) _.every( [true, 1, null, 'yes'], function(value) {

    return value; } ); // => false _.every([true, 1, null, 'yes'], _.identity); // => false
  60. every (alias: all) // Save data if all forms are

    valid if (_.every( view.$(".form"), function (form) { return $(form).valid(); })) { something.save(); }
  61. some (alias: any) Underscore.js docs: Returns true if any of

    the values in the list pass the predicate truth test. Short-circuits and stops traversing the list if a true element is found.
  62. some (alias: any) items = [ { id: 124, status:

    "completed" }, { id: 125, status: "failed" } ]; _.some(items, function (item) { return _.contains( ["failed", "canceled"], item.status ); } );
  63. some (alias: any) items = [ { id: 124, status:

    "completed" }, { id: 125, status: "failed" } ]; _.some(items, function (item) { return _.contains( ["failed", "canceled"], item.status ); } ); // => true
  64. contains (alias: includes) Underscore.js docs: Returns true if the value

    is present in the list. Uses indexOf internally, if list is an Array. Use fromIndex to start your search at a given index.
  65. invoke Underscore.js docs: Calls the method named by methodName on

    each value in the list. Any extra arguments passed to invoke will be forwarded on to the method invocation.
  66. invoke _.invoke( [ [5, 1, 7], [3, 2, 1] ],

    'sort' ); // => [ [1, 5, 7], [1, 2, 3] ]
  67. pluck Underscore.js docs: A convenient version of what is perhaps

    the most common use-case for map: extracting a list of property values.
  68. pluck var items = [ { id: 123, symbol: "foo"

    }, { id: 456, symbol: "bar" }, { id: 789, symbol: "baz" } ]; var symbols = _.pluck(items, "symbol");
  69. pluck var items = [ { id: 123, symbol: "foo"

    }, { id: 456, symbol: "bar" }, { id: 789, symbol: "baz" } ]; var symbols = _.pluck(items, "symbol"); // => ["foo", "bar", "baz"]
  70. max / min Underscore.js docs: Returns the maximum/minimum value in

    list. If an iteratee function is provided, it will be used on each value to generate the criterion by which the value is ranked. -Infinity (+Infinity for mix) is returned if list is empty, so an isEmpty guard may be required.
  71. max / min var items = [ { id: 123,

    priority: 0 }, { id: 456, priority: 9 }, { id: 789, priority: 3 } ]; _.max(items, function (item) { return item.priority; });
  72. max / min var items = [ { id: 123,

    priority: 0 }, { id: 456, priority: 9 }, { id: 789, priority: 3 } ]; _.max(items, function (item) { return item.priority; }); // => { id: 456, priority: 9 }
  73. sortBy Underscore.js docs: Returns a (stably) sorted copy of list,

    ranked in ascending order by the results of running each value through iteratee. iteratee may also be the string name of the property to sort by (eg. length).
  74. sortBy var items = [ { id: 123, priority: 0

    }, { id: 456, priority: 9 }, { id: 789, priority: 3 } ]; _.sortBy(items, 'priority');
  75. sortBy var items = [ { id: 123, priority: 0

    }, { id: 456, priority: 9 }, { id: 789, priority: 3 } ]; _.sortBy(items, 'priority'); // => [ // { id: 123, priority: 0 }, // { id: 789, priority: 3 }, // { id: 456, priority: 9 } // ]
  76. groupBy Underscore.js docs: Splits a collection into sets, grouped by

    the result of running each value through iteratee. If iteratee is a string instead of a function, groups by the property named by iteratee on each of the values.
  77. groupBy _.groupBy([1.3, 2.1, 2.4], function (num) { return Math.floor(num); });

    // => { 1: [1.3], 2: [2.1, 2.4] } _.groupBy( ['alice', 'bob', 'carol'], 'length' );
  78. groupBy _.groupBy([1.3, 2.1, 2.4], function (num) { return Math.floor(num); });

    // => { 1: [1.3], 2: [2.1, 2.4] } _.groupBy( ['alice', 'bob', 'carol'], 'length' ); // => { 3: ["bob"], 5: ["alice", "carol"] }
  79. indexBy Underscore.js docs: Given a list, and an iteratee function

    that returns a key for each element in the list (or a property name), returns an object with an index of each item. Just like groupBy, but for when you know your keys are unique.
  80. indexBy var items = [ { id: 123, priority: 0

    }, { id: 456, priority: 9 }, { id: 789, priority: 3 } ]; _.indexBy(items, 'id');
  81. indexBy var items = [ { id: 123, priority: 0

    }, { id: 456, priority: 9 }, { id: 789, priority: 3 } ]; _.indexBy(items, 'id'); // => [ // "123": { id: 123, priority: 0 }, // "456": { id: 456, priority: 9 }, // "789": { id: 789, priority: 3 } // ]
  82. countBy Underscore.js docs: Sorts a list into groups and returns

    a count for the number of objects in each group. Similar to groupBy, but instead of returning a list of values, returns a count for the number of values in that group.
  83. countBy _.countBy([1, 2, 3, 4, 5], function (num) { return

    num % 2 == 0 ? 'even': 'odd'; }); // => { odd: 3, even: 2 }
  84. sample Underscore.js docs: Produce a random sample from the list.

    Pass a number to return n random elements from the list. Otherwise a single random item will be returned.
  85. sample _.sample([1, 2, 3, 4, 5, 6]); // => 4

    _.sample([1, 2, 3, 4, 5, 6], 3); // => [1, 6, 2]
  86. toArray Underscore.js docs: Creates a real Array from the list

    (anything that can be iterated over). Useful for transmuting the arguments object.
  87. partition Underscore.js docs: Split array into two arrays: one whose

    elements all satisfy predicate and one whose elements all do not satisfy predicate.
  88. partition _.partition( [0, 1, 2, 3, 4, 5], function (num)

    { return num % 2 == 1; } ); // => [ [1, 3, 5], [0, 2, 4] ]
  89. Array Functions compact initial sortedIndex difference intersection union findIndex last

    uniq findLastIndex lastIndexOf unzip first object without flatten range zip indexOf rest
  90. first (alias: head, take) Underscore.js docs: Returns the first element

    of an array. Passing n will return the first n elements of the array.
  91. initial Underscore.js docs: Returns everything but the last entry of

    the array. Especially useful on the arguments object. Pass n to exclude the last n elements from the result.
  92. last Underscore.js docs: Returns the last element of an array.

    Passing n will return the last n elements of the array.
  93. rest (alias: tail, drop) Underscore.js docs: Returns the rest of

    the elements in an array. Pass an index to return the values of the array from that index onward.
  94. compact Underscore.js docs: Returns a copy of the array with

    all falsy values removed. In JavaScript, false, null, 0, "", undefined and NaN are all falsy.
  95. flatten Underscore.js docs: Flattens a nested array (the nesting can

    be to any depth). If you pass shallow, the array will only be flattened a single level.
  96. flatten _.flatten([ 1, [ 2 ], [ 3, [ [

    4 ] ] ] ]); // => [ 1, 2, 3, 4 ]
  97. flatten _.flatten([ 1, [ 2 ], [ 3, [ [

    4 ] ] ] ]); // => [ 1, 2, 3, 4 ] _.flatten([ 1, [ 2 ], [ 3, [ [ 4 ] ] ] ], true);
  98. flatten _.flatten([ 1, [ 2 ], [ 3, [ [

    4 ] ] ] ]); // => [ 1, 2, 3, 4 ] _.flatten([ 1, [ 2 ], [ 3, [ [ 4 ] ] ] ], true); // => [ 1, 2, 3, [ [ 4 ] ] ]
  99. union Underscore.js docs: Computes the union of the passed-in arrays:

    the list of unique items, in order, that are present in one or more of the arrays.
  100. intersection Underscore.js docs: Computes the list of values that are

    the intersection of all the arrays. Each value in the result is present in each of the arrays.
  101. difference Underscore.js docs: Similar to without, but returns the values

    from array that are not present in the other arrays.
  102. uniq (alias: unique) Underscore.js docs: Produces a duplicate-free version of

    the array, using === to test object equality. In particular only the first occurence of each value is kept. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iteratee function.
  103. indexOf Underscore.js docs: Returns the index at which value can

    be found in the array, or -1 if value is not present. If you're working with a large array that is already sorted, pass true for isSorted to use a faster binary search ... or, pass a number as the third argument in order to look for the first matching value in the array after the given index.
  104. lastIndexOf Underscore.js docs: Returns the index of the last occurrence

    of value in the array, or -1 if value is not present. Pass fromIndex to start your search at a given index.
  105. sortedIndex Underscore.js docs: Uses a binary search to determine the

    index at which the value should be inserted into the list in order to maintain the list's sorted order. If an iteratee function is provided, it will be used to compute the sort ranking of each value, including the value you pass. The iteratee may also be the string name of the property to sort by (eg. length).
  106. sortedIndex var events = [ { name: "foo", date: "2015-01-01"

    }, { name: "bar", date: "2015-06-30" } ]; _.sortedIndex( events, { name: "baz", date: "2015-04-01" }, 'date' );
  107. sortedIndex var events = [ { name: "foo", date: "2015-01-01"

    }, { name: "bar", date: "2015-06-30" } ]; _.sortedIndex( events, { name: "baz", date: "2015-04-01" }, 'date' ); // => 1
  108. findIndex Underscore.js docs: Similar to _.indexOf, returns the first index

    where the predicate truth test passes; otherwise returns -1.
  109. findIndex _.findIndex( [1.01, 3.14, 3.5], Number.isInteger ); // => -1

    (not found) _.findIndex( [1.01, 3.14, 3.5, 4], Number.isInteger );
  110. findIndex _.findIndex( [1.01, 3.14, 3.5], Number.isInteger ); // => -1

    (not found) _.findIndex( [1.01, 3.14, 3.5, 4], Number.isInteger ); // => 3
  111. findLastIndex Underscore.js docs: Like _.findIndex but iterates the array in

    reverse, returning the index closest to the end where the predicate truth test passes.
  112. findLastIndex var items = [ { id: 11, status: 'completed'

    }, { id: 21, status: 'in progress' }, { id: 31, status: 'completed' }, { id: 41, status: 'failed' } ]; _.findLastIndex( items, { status: 'completed' } );
  113. findLastIndex var items = [ { id: 11, status: 'completed'

    }, { id: 21, status: 'in progress' }, { id: 31, status: 'completed' }, { id: 41, status: 'failed' } ]; _.findLastIndex( items, { status: 'completed' } ); // => 2
  114. range Underscore.js docs: Creates flexibly-numbered lists of integers, handy for

    each and map loops. start defaults to 0; step defaults to 1. Returns a list of integers from start (inclusive) to stop (exclusive), incremented (or decremented) by step, exclusive. Ranges that stop before they start are considered to be zero-length, not negative — for negative ranges, use a negative step.
  115. range _.range(7); // => [0, 1, 2, 3, 4, 5,

    6] _.range(5, 11); // => [5, 6, 7, 8, 9, 10]
  116. range _.range(7); // => [0, 1, 2, 3, 4, 5,

    6] _.range(5, 11); // => [5, 6, 7, 8, 9, 10] _.range(0, 100, 20);
  117. range _.range(7); // => [0, 1, 2, 3, 4, 5,

    6] _.range(5, 11); // => [5, 6, 7, 8, 9, 10] _.range(0, 100, 20); // => [0, 20, 40, 60, 80]
  118. range _.range(7); // => [0, 1, 2, 3, 4, 5,

    6] _.range(5, 11); // => [5, 6, 7, 8, 9, 10] _.range(0, 100, 20); // => [0, 20, 40, 60, 80] _.range(0, -7, -1)
  119. range _.range(7); // => [0, 1, 2, 3, 4, 5,

    6] _.range(5, 11); // => [5, 6, 7, 8, 9, 10] _.range(0, 100, 20); // => [0, 20, 40, 60, 80] _.range(0, -7, -1) // => [0, -1, -2, -3, -4, -5, -6]