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:
Robert Mennell
2018-08-16 13:30:23 -07:00
committed by GitHub
2 changed files with 3 additions and 3 deletions

View File

@ -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

View File

@ -1,2 +1,2 @@
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];
module.exports = nthElement;