From ec240463b51f0059b7b4e2c83cdefc8b4784d267 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 23:29:12 +0200 Subject: [PATCH] Tag, lint, build --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ tag_database | 3 +++ 2 files changed, 47 insertions(+) diff --git a/README.md b/README.md index 90e814bc8..367898d9e 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ * [Initialize array with values](#initialize-array-with-values) * [Last of list](#last-of-list) * [Median of array of numbers](#median-of-array-of-numbers) +* [Nth element of array](#nth-element-of-array) * [Pick](#pick) * [Shuffle array](#shuffle-array) * [Similarity between arrays](#similarity-between-arrays) @@ -71,6 +72,7 @@ * [Hamming distance](#hamming-distance) * [Percentile](#percentile) * [Powerset](#powerset) +* [Round number to n digits](#round-number-to-n-digits) * [Standard deviation](#standard-deviation) ### Media @@ -79,6 +81,7 @@ ### Object * [Object from key value pairs](#object-from-key-value-pairs) * [Object to key value pairs](#object-to-key-value-pairs) +* [Shallow clone object](#shallow-clone-object) ### String * [Anagrams of string (with duplicates)](#anagrams-of-string-with-duplicates) @@ -394,6 +397,20 @@ const median = arr => { [⬆ back to top](#table-of-contents) +### Nth element of 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 nth = (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' +``` + +[⬆ back to top](#table-of-contents) + ### Pick Use `Array.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj. @@ -798,6 +815,18 @@ const powerset = arr => [⬆ back to top](#table-of-contents) +### Round number to n digits + +Use `Math.round()` and template literals to round the number to the specified number of digits. +Omit the second argument, `decimals` to round to an integer. + +```js +const round = (n, decimals=0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`); +// round(1.005, 2) -> 1.01 +``` + +[⬆ back to top](#table-of-contents) + ### Standard deviation Use `Array.reduce()` to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then @@ -858,6 +887,21 @@ const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); // objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]]) ``` +[⬆ back to top](#table-of-contents) + +### Shallow clone object + +Use the object `...spread` operator to spread the properties of the target object into the clone. + +```js +const shallowClone = obj => ({ ...obj }); +/* +const a = { x: true, y: 1 }; +const b = shallowClone(a); +a === b -> false +*/ +``` + [⬆ back to top](#table-of-contents) ## String diff --git a/tag_database b/tag_database index 7f81cbec1..b2535a484 100644 --- a/tag_database +++ b/tag_database @@ -49,6 +49,7 @@ is-symbol:utility last-of-list:array measure-time-taken-by-function:utility median-of-array-of-numbers:array +nth-element-of-array:array number-to-array-of-digits:utility object-from-key-value-pairs:object object-to-key-value-pairs:object @@ -63,8 +64,10 @@ random-number-in-range:utility redirect-to-URL:browser reverse-a-string:string RGB-to-hexadecimal:utility +round-number-to-n-digits:math run-promises-in-series:function scroll-to-top:browser +shallow-clone-object:object shuffle-array:array similarity-between-arrays:array sleep:function