mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Merge pull request #285 from malclocke/add_index_argument_to_last
Add an optional index argument to _.last()
This commit is contained in:
@@ -26,8 +26,12 @@ $(document).ready(function() {
|
|||||||
|
|
||||||
test("arrays: last", function() {
|
test("arrays: last", function() {
|
||||||
equals(_.last([1,2,3]), 3, 'can pull out the last element of an array');
|
equals(_.last([1,2,3]), 3, 'can pull out the last element of an array');
|
||||||
|
equals(_.last([1,2,3], 0).join(', '), "", 'can pass an index to last');
|
||||||
|
equals(_.last([1,2,3], 2).join(', '), '2, 3', 'can pass an index to last');
|
||||||
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
|
var result = (function(){ return _(arguments).last(); })(1, 2, 3, 4);
|
||||||
equals(result, 4, 'works on an arguments object');
|
equals(result, 4, 'works on an arguments object');
|
||||||
|
result = _.map([[1,2,3],[1,2,3]], _.last);
|
||||||
|
equals(result.join(','), '3,3', 'works well with _.map');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("arrays: compact", function() {
|
test("arrays: compact", function() {
|
||||||
|
|||||||
@@ -306,9 +306,10 @@
|
|||||||
return slice.call(array, (index == null) || guard ? 1 : index);
|
return slice.call(array, (index == null) || guard ? 1 : index);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the last element of an array.
|
// Get the last element of an array. Passing **n** will return the last N
|
||||||
_.last = function(array) {
|
// values in the array. The **guard** check allows it to work with `_.map`.
|
||||||
return array[array.length - 1];
|
_.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.
|
// Trim out all falsy values from an array.
|
||||||
|
|||||||
Reference in New Issue
Block a user