The spread operator allows you to quite literally “spread” out an array. This can be used to transform an array into a list of arguments or even combine two arrays together. You could also use it to form a list of arguments to a function too. Check it out: In the first example we show […]
Drop list elements from the left
Creates a new array with n elements removed from the left. Source https://www.30secondsofcode.org/js/s/drop
Get the height of the viewport
There are two methods to get the viewport height: window.innerHeight and document.documentElement.clientHeight. The former is more accurate. The latter has better browser support. To get the best of both worlds, try innerHeight first, and fallback to clientHeight if not supported. Source https://vanillajstoolkit.com/reference/viewport/viewport-height/
Assign keys to an object with the same name
When you are assigning keys to an object, if the key is the same name as the variable that holds the value you want to assign you can omit the value assignment altogether. This prevents you from having to repeat yourself, something we all hate doing. Let’s take a look at an example using some […]
Drop list elements from the right
Creates a new array with n elements removed from the right. Source https://www.30secondsofcode.org/js/s/drop-right
Insert an element at the end of a set of elements inside a shared parent
Call the append() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-append/
Destructing variable assignment
Assigning variables from an array one-by-one is time consuming and silly. Just use destructuring assignment to accomplish this quicker and easier: Source https://levelup.gitconnected.com/6-javascript-code-snippets-for-solving-common-problems-33deb6cacef3
Find last n matches
Finds the last n elements for which the provided function returns a truthy value. Use a for loop to execute the provided matcher for each element of arr. Use Array.prototype.unshift() to prepend elements to the results array and return them if its length is equal to n. Source https://www.30secondsofcode.org/js/s/find-last-n
Insert an element at the beginning of a set elements inside a shared parent
Call the prepend() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-prepend/
Filtering an array of objects based on a condition
If you have a large array of data and want to filter out items based on a specific condition then you can simply use the filter function. In this example we have an array of file paths. Some files are in ‘dir1’ while others are in ‘dir2’. Let’s say we want to filter for only a specific directory: Filtering for […]