- Remove duplicates from an array of numbers/strings
- A simple search (case-sensitive)
- A simple search (case-insensitive)
- Check if any of the users have admin rights
- Flattening an array of arrays
- Create an object that contains the frequency of the specified key
- Indexing an array of objects (lookup table)
- Extract the unique values for the given key of each item in the array
- Object key-value map reversal
- Object key-array-value map reverse
- Create an array of Fahrenheit values from an array of Celsius values
- Encode an object into a query string
- Print a table of users as a readable string only with specified keys
- Find and replace a key-value pair in an array of objects
- Union (A ∪ B) of arrays
- Intersection (A ∩ B) of arrays
Last active
May 2, 2021 07:50
-
-
Save griimick/ac161c717d2bf1190881f406a09ba88f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
const findQuery = record => record.id != 47; | |
const udpateQuery = record => {...record, age: record.age + 1}; | |
const updatedUsers = users.map( | |
p => findQuery(p) ? p : updateQuery(p); | |
); | |
/* | |
Downside is that it itereates through the complete array everytime | |
Create a new shallow copy of the array. So this update is not inpalce | |
*/ | |
console.log(updatedUsers); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const celsius = [-15, -5, 0, 10, 16, 20, 24, 32] | |
const fahrenheit = celsius.map(t => t * 1.8 + 32); | |
console.log(fahrenheit); | |
//[5, 23, 32, 50, 60.8, 68, 75.2, 89.6] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
let result = users.some(u => u.group === 'admin'); | |
console.log(result); | |
// true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nested = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | |
// Bad for performance as we spreading the array every time and creating a new array | |
let flat = nested.reduce((acc, curr) => [...acc, ...curr], []); | |
let betterFlat = [].concat.apply([], nested); | |
console.log(flat); | |
// [1, 2, 3, 4, 5, 6, 7, 8, 9] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
const ageGroups = users.reduce((acc, curr) => { | |
acc[curr.age] = acc[curr.age] + 1 || 1; | |
return acc; | |
}, {}) | |
console.log(ageGroups); | |
// {23: 1, 28: 2, 34: 1} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
const indexes = users.reduce((acc, it) => (acc[it.id] = it, acc), {}); | |
console.log(indexes); | |
/* | |
{ | |
11: { id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
47: { id: 47, name: 'John', age: 28, group: 'admin' }, | |
85: { id: 85, name: 'William', age: 34, group: 'editor' }, | |
97: { id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
} | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrA = [1, 4, 3, 2]; | |
const arrB = [5, 2, 6, 7, 1]; | |
arrA.filter(it => arrB.includes(it)); | |
// [1,2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = { | |
a : [1, 2, 3], | |
b : [1, 3], | |
c : [2, 3], | |
d : [2] | |
}; | |
const output = Object.keys(input).reduce((acc, curr) => { | |
let values = input[curr]; | |
acc = values.reduce((a, c) => { | |
a[c] = a[c] || []; | |
a[c].push(curr); | |
return acc; | |
}, acc); | |
return acc; | |
}, {}); | |
console.log(output); | |
/* | |
{ | |
1: ['a', 'b'], | |
2: ['a', 'c', 'd'], | |
3: ['a', 'b', 'c'] | |
} | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cities = { | |
Lyon: 'France', | |
Berlin: 'Germany', | |
Paris: 'France' | |
}; | |
let result = Object.keys(cities).reduce((acc, curr) => { | |
let country = cities[curr]; | |
acc[country] = acc[country] || []; | |
acc[country].push(curr); | |
return acc; | |
}, {}); | |
console.log(result); | |
/* | |
{ | |
France: ["Lyon", "Paris"], | |
Germany: ["Berlin"] | |
} | |
*/ | |
let ONE_LINER = Object.keys(cities).reduce( | |
(acc, k) => (acc[cities[k]] = [...(acc[cities[k]] || []), k], acc) , {}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const params = {lat: 45, lng: 6, alt: 1000}; | |
const queryString = Object.entries(params).map([k, v] => encodeURIComponent(k) + "=" + encodeURIComponent(v)).join('&'); | |
console.log(queryString); | |
// "lat=45&lng=6&alt=1000" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const values = [3, 1, 3, 5, 2, 4, 4]; | |
const uniqueValues = [...new Set(values)]; | |
console.log(uniqueValues); | |
// [3, 1, 5, 2, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
let query = "joh"; | |
let res = users.filter(u => u.name.includes(query)); | |
console.log(res); | |
// [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
let query = "joh"; | |
let res = users.filter(u => new RegExp(query, "i").test(u.name)); | |
console.log(res); | |
// [{ id: 47, name: 'John', age: 28, group: 'admin' }] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
users.map(({id, age, group}) => `\n${id} ${age} ${group}`).join('') | |
console.log(output); | |
/* | |
" | |
11 23 editor | |
47 28 admin | |
85 34 editor | |
97 28 admin" | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrA = [1, 4, 3, 2]; | |
const arrB = [5, 2, 6, 7, 1]; | |
[...new Set([...arrA, ...arrB])]; | |
//[1, 4, 3, 2, 5, 6, 7] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const users = [ | |
{ id: 11, name: 'Adam', age: 23, group: 'editor' }, | |
{ id: 47, name: 'John', age: 28, group: 'admin' }, | |
{ id: 85, name: 'William', age: 34, group: 'editor' }, | |
{ id: 97, name: 'Oliver', age: 28, group: 'admin' } | |
]; | |
let uniqueGroups = [...new Set(users.map(u => u.group))]; | |
console.log(uniqueGroups); | |
// ['editor', 'admin'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment