Add guard check to _.last()

Allows _.last() to work as expected with _.map().
This commit is contained in:
Malcolm Locke
2011-09-01 01:10:10 +12:00
parent a8f0445192
commit e449b00a26
2 changed files with 5 additions and 3 deletions

View File

@@ -307,9 +307,9 @@
};
// Get the last element of an array. Passing **n** will return the last N
// values in the array.
_.last = function(array, n) {
return (n != null) ? slice.call(array, array.length - n) : array[array.length - 1];
// 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.