Skip to content

Instantly share code, notes, and snippets.

@griimick
Last active May 2, 2021 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save griimick/ac161c717d2bf1190881f406a09ba88f to your computer and use it in GitHub Desktop.
Save griimick/ac161c717d2bf1190881f406a09ba88f to your computer and use it in GitHub Desktop.
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);
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]
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
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]
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}
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' }
}
*/
const arrA = [1, 4, 3, 2];
const arrB = [5, 2, 6, 7, 1];
arrA.filter(it => arrB.includes(it));
// [1,2]
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']
}
*/
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) , {});
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"
const values = [3, 1, 3, 5, 2, 4, 4];
const uniqueValues = [...new Set(values)];
console.log(uniqueValues);
// [3, 1, 5, 2, 4]
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);
// []
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' }]
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"
*/
const arrA = [1, 4, 3, 2];
const arrB = [5, 2, 6, 7, 1];
[...new Set([...arrA, ...arrB])];
//[1, 4, 3, 2, 5, 6, 7]
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