Merge pull request #712 from Bruce-Feldman/Simplify-nth_Element
[ENHANCEMENT] Simplified nth Element, added better support for negative numbers thanks to @Bruce-Feldman
This commit is contained in:
@ -3,11 +3,11 @@
|
||||
Returns the nth element of an array.
|
||||
|
||||
Use `Array.slice()` to get an array containing the nth element at the first place.
|
||||
If the index is out of bounds, return `[]`.
|
||||
If the index is out of bounds, return `undefined`.
|
||||
Omit the second argument, `n`, to get the first element of the array.
|
||||
|
||||
```js
|
||||
const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
|
||||
const nthElement = (arr, n = 0) => (n === -1 ? arr.slice(n) : arr.slice(n, n + 1))[0];
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user