Professional Documents
Culture Documents
20 JavaScript Snippets That Will Make You A Better Developer - by Henrik Larsen Toft - Jun, 2022 - Level Up Coding
20 JavaScript Snippets That Will Make You A Better Developer - by Henrik Larsen Toft - Jun, 2022 - Level Up Coding
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Save
Although most of these functions are implemented in different libraries — it’s important
to understand how and when to break difficult problems into smaller ones. This way of
thinking about problem solving will increase your productivity and make you a better
developer.
Get value
Given an object or array — the function will return the value at specified path,
otherwise null.
getValue.js
hosted with ❤ by GitHub view raw
Clamp Open in app Get started
Ensure a value is within a specified range, otherwise “clamp” to the closest of the minimum
clamp.js
hosted with ❤ by GitHub view raw
Sleep
Wait the specified duration in milliseconds before performing the next operation.
sleep.js
hosted with ❤ by GitHub Open in app Get started
view raw
Group by
Group and index related items in an object according to the keying-function.
groupBy.js
hosted with ❤ by GitHub view raw
Open in app Get started
Collect By
Create sub-lists containing related items according to the keying-function.
collectBy.js
hosted with ❤ by GitHub view raw
Open in app Get started
Head
Get the first element of a list. This function is useful for writing clean and readable code.
head.js
hosted with ❤ by GitHub view raw
Tail
Get all but the first element of a list. This function is useful for writing clean and readable
code.
tail.js
hosted with ❤ by GitHub view raw
288 3
Flatten
Create a flat list by pulling all items from nested sub-lists recursively.
1 const flatten = list => list.reduce((prev, next) => ([ Open in app Get started
2 ...prev,
3 ...(Array.isArray(next) ? flatten(next) : [next])
4 ]), []);
5
6 flatten([[1, 2, [3, 4], 5, [6, [7, 8]]]]); // = [1, 2, 3, 4, 5, 6, 7, 8]
flatten.js
hosted with ❤ by GitHub view raw
Intersection By
Find all values that are present in both lists as defined by a keying-function.
intersectionBy.js
hosted with ❤ by GitHub view raw
Index By
Index each element in a list by a value determined by the keying-function.
1 const indexBy = (fn, list) => Open in app Get started
2 list.reduce((prev, next) => ({
3 ...prev,
4 [fn(next)]: next
5 }), {});
6
7 indexBy(val => val.a, [{ a: 1 }, { a: 2 }, { a: 3 }]);
8 // = { 1: { a: 1 }, 2: { a:2 }, 3: { a: 3 } }
9
indexBy.js
hosted with ❤ by GitHub view raw
Difference By
Find all items in the first list that are not present in the second list — determined by the
keying-function.
differenceBy.js
hosted with ❤ by GitHub view raw
Open in app Get started
Recover With
Return the default value if the given function throws an Error .
recoverWith.js
hosted with ❤ by GitHub view raw
Open in app Get started
Distance
Calculate the Euclidean distance between two points p1 & p2 .
distance.js
hosted with ❤ by GitHub view raw
Drop While
Drops elements from the list, beginning at the first element, until som predicate is met.
dropWhile.js
hosted with ❤ by GitHub view raw
Open in app Get started
Sum By
Calculate the sum of all elements in a list given some function that produce the individual
value of each element.
sumBy.js
hosted with ❤ by GitHub view raw
Ascending
Creates a ascending comparator-function given a valuating function.
Descending
Creates a descending comparator-function given a valuating function.
descending.js
hosted with ❤ by GitHub view raw
Find Key
Find the first key within an index that satisfies the given predicate
Find the first key within an index that satisfies the given predicate.
Open in app Get started
findKey.js
hosted with ❤ by GitHub view raw
Bifurcate By
Split the values of a given list into two lists, one containing values the predicate function
evaluates to truthy, the other list containing the falsy.
Pipe
Perform left-to-right function composition. All extra arguments will be passed to the first
function in the list, thus can have any arity. The result will be passed on the second, and
the result of the second will be passed to third,… and so on until all functions have been
processed.
pipe.js
hosted with ❤ by GitHub view raw
Final Thoughts Open in app Get started
Though all these functions can be really useful to help you solve the problems you are
working on — the most important lesson is knowing how to break complex and difficult
problems into small well defined problems that can be solved independently. Once you
master this — then you are already on your way of becoming an excellent developer!