Re-implement the following methods from underscore. You can reference the Underscore documentation for what they do as well as example usage. Underscore offers optional arguments for some of these methods. In those cases, follow the instructions here, rather than the Underscore documentation.
-
_.each
takes a collection and an iterator function and calls theiterator(value, key, collection)
for each element of collection. It should accept both arrays and objects -
_.indexOf
takes an array and a target value. It returns the index at which value can be found in the array, or -1 if value is not present in the array. Use_.each
in your implementation -
_.filter
takes a collection and a predicate function, also known as a truth test. Return an array of all elements of the collection that pass the truth test, ie return a truthy value from the predicate function. Consider reusing_.each
here. -
_.reject
works similiarly to_.filter
but returns an array of all elements that don't pass the truth test. Use_.filter
in your implementation. -
_.uniq
produces a duplicate free version of an array. Use_.filter
and_.indexOf
in your implementation. -
_.map
works a lot like_.each
. It takes a collection and iterator, and calls the iterator on every element of the array. However,_.map
returns an results array of the transformed values. -
_.pluck
takes an array of objects and returns an array of the values of a certain property in it. Ex. take an array of people objects and return an array of just their ages. Use_.map
in your implementation. -
_.reduce
reduces an array or object to a single value by repetitively callingiterator(accumulator, item)
for each item.accumulator
should be the return value of the previous iterator call. Reduce takes a collection, an iterator, and an optional third argument of a starting value. Where no starting value is provided, the first element is used as the starting value and the iterator is not called on the first value. -
_.every
determines whether all ements of a collection pass a truth test. It takes a collection and a predicate function. Use_.reduce
in your implementation. -
_.some
determines whether some eleements of a collection pass a truth test. It takes a collection and a predicate function. You could use_.reduce
here, but there is a clever way to re-use_.every
.