Merge pull request #285 from malclocke/add_index_argument_to_last

Add an optional index argument to _.last()
This commit is contained in:
Jeremy Ashkenas
2011-10-04 13:06:07 -07:00
2 changed files with 8 additions and 3 deletions

View File

@@ -306,9 +306,10 @@
return slice.call(array, (index == null) || guard ? 1 : index);
};
// Get the last element of an array.
_.last = function(array) {
return array[array.length - 1];
// Get the last element of an array. Passing **n** will return the last N
// values in the array. The **guard** check allows it to work with `_.map`.
_.last = function(array, n, guard) {
return (n != null) && !guard ? slice.call(array, array.length - n) : array[array.length - 1];
};
// Trim out all falsy values from an array.