Collections Flashcards
Learn the collection based functions of Underscore.js
_.each(list, iteratee, [context])
Iterates over a list of elements, yielding each in turn to an iteratee function. The iteratee is bound to the context object, if one is passed. iter: function(element, index, list) iter obj: function(value, key, list)
_.map(list, iteratee, [context])
Produces a new array of values by mapping each value in list through a transformation function (iteratee). iteratee: function(value, index, list)
_.reduce(list, iteratee, [memo], [context])
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. iteratee: function(memo, value, index, list)
_.reduceRight(list, iteratee, memo, [context])
The right-associative version of reduce. (Opposite direction)
_.find(list, predicate, [context])
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.
_.filter(list, predicate, [context])
Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).
_.where(list, properties)
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.
_.findWhere(list, properties)
Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.
_.reject(list, predicate, [context])
Returns the values in list without the elements that the truth test (predicate) passes. The opposite of filter.
_.every(list, [predicate], [context])
Returns true if all of the values in the list pass the predicate truth test.
_.some(list, [predicate], [context])
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.
_.contains(list, value, [fromIndex])
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.
_.invoke(list, methodName, *arguments)
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.
_.pluck(list, propertyName)
A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.
_.max(list, [iteratee], [context])
Returns the maximum 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.
_.min(list, [iteratee], [context])
Returns the 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.
_.sortBy(list, iteratee, [context])
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).
_.groupBy(list, iteratee, [context])
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.
_.indexBy(list, iteratee, [context])
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.
_.countBy(list, iteratee, [context])
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.