Perform a shallow merge of two or more objects into the first. Pass in each object to merge as an argument. Note: in a shallow merge, nested objects are overwritten completely rather than having their values merged together. All objects are merged into the first. To create a new object, pass in an empty object as […]
Convert a string to an array of characters
Use the spread operator (…) to convert the string into an array of characters. Source https://www.30secondsofcode.org/js/s/to-char-array
Date of yesterday
Results in a string representation of yesterday’s date. Use the Date constructor to get the current date. Decrement it by one using Date.prototype.getDate() and set the value to the result using Date.prototype.setDate(). Use Date.prototype.toISOString() to return a string in yyyy-mm-dd format. Source https://www.30secondsofcode.org/js/s/yesterday
Vanilla JS toggle
Source https://github.com/roeib/JavaScript-snippets#vanilla-js-toggle
Get an array of key/value pairs from an object
Return an array of key/value pairs, also represented as arrays. Pass in the object as an argument. Source https://vanillajstoolkit.com/reference/objects/object-entries/
What is the difference between prefix and postfix operators?
The increment operator (++) adds 1 to its operand and returns a value. Similarly, the decrement operator (–) subtracts 1 from its operand and returns a value. Both of these operators can be used either prefix (++i, –i) or postfix (i++, i–). If used prefix, the value is incremented/decremented, and the value of the expression is the updated value. If used […]
Fire an event listener only once
Source https://github.com/roeib/JavaScript-snippets#fire-an-event-listener-only-once
Return an array of keys from an object
Return an array of keys from an object. Pass in the object as an argument. Source https://vanillajstoolkit.com/reference/objects/object-keys/
Get the index of the first matching item in an array
Get the index of the first matching item in an array. Returns -1 if no matching item is found. Source https://vanillajstoolkit.com/reference/arrays/array-indexof.md/
Sorting by an Object Property
You can sort arrays of objects. In this case we leverage the collator.compare method and pass along the properties that we want to sort by. Source https://elijahmanor.com/byte/js-locale-sort