Collections Flashcards

Learn the collection based functions of Underscore.js

1
Q

_.each(list, iteratee, [context])

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

_.map(list, iteratee, [context])

A

Produces a new array of values by mapping each value in list through a transformation function (iteratee). iteratee: function(value, index, list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

_.reduce(list, iteratee, [memo], [context])

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

_.reduceRight(list, iteratee, memo, [context])

A

The right-associative version of reduce. (Opposite direction)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

_.find(list, predicate, [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

_.filter(list, predicate, [context])

A

Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

_.where(list, properties)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

_.findWhere(list, properties)

A

Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

_.reject(list, predicate, [context])

A

Returns the values in list without the elements that the truth test (predicate) passes. The opposite of filter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

_.every(list, [predicate], [context])

A

Returns true if all of the values in the list pass the predicate truth test.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

_.some(list, [predicate], [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

_.contains(list, value, [fromIndex])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

_.invoke(list, methodName, *arguments)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

_.pluck(list, propertyName)

A

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

_.max(list, [iteratee], [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

_.min(list, [iteratee], [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

_.sortBy(list, iteratee, [context])

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

_.groupBy(list, iteratee, [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

_.indexBy(list, iteratee, [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

_.countBy(list, iteratee, [context])

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

_.shuffle(list)

A

Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle.

22
Q

_.sample(list, [n])

A

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.

23
Q

_.toArray(list)

A

Creates a real Array from the list (anything that can be iterated over). Useful for transmuting the arguments object.

24
Q

_.size(list)

A

Return the number of values in the list.

25
Q

_.partition(array, predicate)

A

Split array into two arrays: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.

26
Q

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)

A

_.each(list, iteratee, [context])

27
Q

Produces a new array of values by mapping each value in list through a transformation function (iteratee). iteratee: function(value, index, list)

A

_.map(list, iteratee, [context])

28
Q

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)

A

_.reduce(list, iteratee, [memo], [context])

29
Q

The right-associative version of reduce. (Opposite direction)

A

_.reduceRight(list, iteratee, memo, [context])

30
Q

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.

A

_.find(list, predicate, [context])

31
Q

Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).

A

_.filter(list, predicate, [context])

32
Q

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.

A

_.where(list, properties)

33
Q

Looks through the list and returns the first value that matches all of the key-value pairs listed in properties.

A

_.findWhere(list, properties)

34
Q

Returns the values in list without the elements that the truth test (predicate) passes. The opposite of filter.

A

_.reject(list, predicate, [context])

35
Q

Returns true if all of the values in the list pass the predicate truth test.

A

_.every(list, [predicate], [context])

36
Q

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.

A

_.some(list, [predicate], [context])

37
Q

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.

A

_.contains(list, value, [fromIndex])

38
Q

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.

A

_.invoke(list, methodName, *arguments)

39
Q

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

A

_.pluck(list, propertyName)

40
Q

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.

A

_.max(list, [iteratee], [context])

41
Q

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.

A

_.min(list, [iteratee], [context])

42
Q

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).

A

_.sortBy(list, iteratee, [context])

43
Q

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.

A

_.groupBy(list, iteratee, [context])

44
Q

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.

A

_.indexBy(list, iteratee, [context])

45
Q

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.

A

_.countBy(list, iteratee, [context])

46
Q

Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle.

A

_.shuffle(list)

47
Q

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.

A

_.sample(list, [n])

48
Q

Creates a real Array from the list (anything that can be iterated over). Useful for transmuting the arguments object.

A

_.toArray(list)

49
Q

Return the number of values in the list.

A

_.size(list)

50
Q

Split array into two arrays: one whose elements all satisfy predicate and one whose elements all do not satisfy predicate.

A

_.partition(array, predicate)