Similarity between arrays, object from key-value pairs
This commit is contained in:
22
README.md
22
README.md
@ -23,12 +23,14 @@
|
|||||||
* [Initialize array with range](#initialize-array-with-range)
|
* [Initialize array with range](#initialize-array-with-range)
|
||||||
* [Initialize array with values](#initialize-array-with-values)
|
* [Initialize array with values](#initialize-array-with-values)
|
||||||
* [Measure time taken by function](#measure-time-taken-by-function)
|
* [Measure time taken by function](#measure-time-taken-by-function)
|
||||||
|
* [Object from key value pairs](#object-from-key-value-pairs)
|
||||||
* [Powerset](#powerset)
|
* [Powerset](#powerset)
|
||||||
* [Random number in range](#random-number-in-range)
|
* [Random number in range](#random-number-in-range)
|
||||||
* [Randomize order of array](#randomize-order-of-array)
|
* [Randomize order of array](#randomize-order-of-array)
|
||||||
* [Redirect to url](#redirect-to-url)
|
* [Redirect to url](#redirect-to-url)
|
||||||
* [RGB to hexadecimal](#rgb-to-hexadecimal)
|
* [RGB to hexadecimal](#rgb-to-hexadecimal)
|
||||||
* [Scroll to top](#scroll-to-top)
|
* [Scroll to top](#scroll-to-top)
|
||||||
|
* [Similarity between arrays](#similarity-between-arrays)
|
||||||
* [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical)
|
* [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical)
|
||||||
* [Sum of array of numbers](#sum-of-array-of-numbers)
|
* [Sum of array of numbers](#sum-of-array-of-numbers)
|
||||||
* [Swap values of two variables](#swap-values-of-two-variables)
|
* [Swap values of two variables](#swap-values-of-two-variables)
|
||||||
@ -89,7 +91,7 @@ var currentUrl = _ => window.location.href;
|
|||||||
|
|
||||||
### Difference between arrays
|
### Difference between arrays
|
||||||
|
|
||||||
Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`.
|
Use `filter()` to remove values that are part of `values`, determined using `indexOf()`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var difference = (arr, values) =>
|
var difference = (arr, values) =>
|
||||||
@ -190,6 +192,15 @@ var timeTaken = (f,...args) => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Object from key-value pairs
|
||||||
|
|
||||||
|
Use `map()` to create objects for each key-value pair, combine with `Object.assign()`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var objectFromPairs = arr =>
|
||||||
|
Object.assign(...arr.map( v => {return {[v[0]] : v[1]};} ));
|
||||||
|
```
|
||||||
|
|
||||||
### Powerset
|
### Powerset
|
||||||
|
|
||||||
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.
|
Use `reduce()` combined with `map()` to iterate over elements and combine into an array containing all combinations.
|
||||||
@ -250,6 +261,15 @@ var scrollToTop = _ => {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Similarity between arrays
|
||||||
|
|
||||||
|
Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var difference = (arr, values) =>
|
||||||
|
arr.filter(v => values.indexOf(v) !== -1);
|
||||||
|
```
|
||||||
|
|
||||||
### Sort characters in string (alphabetical)
|
### Sort characters in string (alphabetical)
|
||||||
|
|
||||||
Split the string using `split('')`, `sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
Split the string using `split('')`, `sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### Difference between arrays
|
### Difference between arrays
|
||||||
|
|
||||||
Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`.
|
Use `filter()` to remove values that are part of `values`, determined using `indexOf()`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var difference = (arr, values) =>
|
var difference = (arr, values) =>
|
||||||
|
|||||||
8
snippets/object-from-key-value-pairs.md
Normal file
8
snippets/object-from-key-value-pairs.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
### Object from key-value pairs
|
||||||
|
|
||||||
|
Use `map()` to create objects for each key-value pair, combine with `Object.assign()`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var objectFromPairs = arr =>
|
||||||
|
Object.assign(...arr.map( v => {return {[v[0]] : v[1]};} ));
|
||||||
|
```
|
||||||
8
snippets/similarity-between-arrays.md
Normal file
8
snippets/similarity-between-arrays.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
### Similarity between arrays
|
||||||
|
|
||||||
|
Use `filter()` to remove values that are not part of `values`, determined using `indexOf()`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var difference = (arr, values) =>
|
||||||
|
arr.filter(v => values.indexOf(v) !== -1);
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user