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
Miscellaneous
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 […]
Insert an element into the DOM after another one
Insert an element in the DOM after another one. Call the after() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-after/
Loop over an object’s keys and values
Sometimes your data structure might be a complex object that contains a bunch of key/value pairs. Iterating over each pair is a little odd at first glance depending on what languages you’re used to but its straightforward once you get used to using the functions of Object. After you grab the objects keys you can loop […]
Sorting Objects with a Primary and Secondary Property
When you sort an array and have several matches exact matches, it is handy to have a secondary property to sort by to break the tie. You can use the same approach as above, but with a little more logic. Inside the compareFunction, if the two properties have the same value (a zero compare value), then […]
Find a specific object in an array of objects
This is arguably one of the most common tasks you’ll need to accomplish in the JS world. Iterating through an array of objects to find a specific one. The find method is our friend here. Simply plugin the selection criteria using an anonymous function as the argument and you’re set: Source https://levelup.gitconnected.com/6-javascript-code-snippets-for-solving-common-problems-33deb6cacef3
Check if all array elements are unique
Checks if all elements in an array are unique. Create a new Set from the mapped values to keep only unique occurrences. Use Array.prototype.length and Set.prototype.size to compare the length of the unique values to the original array. Source https://www.30secondsofcode.org/js/s/all-unique
Insert an element before another one
Insert an element before another one. Call the before() method on the reference node, and pass in the new node as an argument. Source https://vanillajstoolkit.com/reference/dom-injection/element-before/
Deep clone a value in Node.js
If you want to deep clone a value in Node.js, you no longer need to use a library or the JSON.parse(JSON.stringify(value)) hack. You can use the new global function structuredClone() Source https://github.com/roeib/JavaScript-snippets#structuredclone