Merge pull request #106 from kingdavidmartins/add-take-snippet

ran npm run build-list & add take.md
This commit is contained in:
Angelos Chalaris
2017-12-14 11:55:12 +02:00
committed by GitHub
2 changed files with 23 additions and 1 deletions

View File

@ -57,7 +57,7 @@
* [Promisify](#promisify)
* [Random integer in range](#random-integer-in-range)
* [Random number in range](#random-number-in-range)
* [Redirect to URL](#redirect-to-url)
* [Redirect to url](#redirect-to-url)
* [Reverse a string](#reverse-a-string)
* [RGB to hexadecimal](#rgb-to-hexadecimal)
* [Run promises in series](#run-promises-in-series)
@ -70,6 +70,7 @@
* [Sum of array of numbers](#sum-of-array-of-numbers)
* [Swap values of two variables](#swap-values-of-two-variables)
* [Tail of list](#tail-of-list)
* [Take](#take)
* [Truncate a string](#truncate-a-string)
* [Unique values of array](#unique-values-of-array)
* [URL parameters](#url-parameters)
@ -743,6 +744,17 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr;
// tail([1]) -> [1]
```
### Take
Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning.
```js
const take = (arr, n = 1) => arr.slice(0, n);
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
```
### Truncate a String
Determine if the string's `length` is greater than `num`.

10
snippets/take.md Normal file
View File

@ -0,0 +1,10 @@
### Take
Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning.
```js
const take = (arr, n = 1) => arr.slice(0, n);
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
```