_.last
Returns the last element of an array. Passing n will return the last n elements of the array.
- // Underscore/Lodash
- const numbers = [1, 2, 3, 4, 5];
- _.last(numbers);
- // => 5
- _.last(numbers, 2);
- // => [4, 5]
- // Native
- const numbers = [1, 2, 3, 4, 5];
- numbers[numbers.length - 1];
- // => 5
- //or
- numbers.slice(-1)[0];
- // => 5
- //or
- [].concat(numbers).pop()
- // => 5
- // Native (works even with potentially undefined/null)
- [].concat(undefined).pop()
- // => undefined
- numbers.slice(-2);
- // => [4, 5]
Browser Support for Array.prototype.concat()
1.0 ✔ | ✔ | 1.0 ✔ | 5.5 ✔ | ✔ | ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .