Fully renamed and updated everything, tagged, built
This commit is contained in:
13
snippets/nthElement.md
Normal file
13
snippets/nthElement.md
Normal file
@ -0,0 +1,13 @@
|
||||
### nthElement
|
||||
|
||||
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 `[]`.
|
||||
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];
|
||||
// nth(['a','b','c'],1) -> 'b'
|
||||
// nth(['a','b','b']-2) -> 'a'
|
||||
```
|
||||
Reference in New Issue
Block a user