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