To check if a key exists in a js object, you can: compare the key’s value to undefined, use .hasOwnProperty, or use “key in object” syntax. Each method behaves a bit differently, so let’s do some examples.
Archives for October 2023
Replace a string in Python
To replace a string or substring in Python you have two main options: Python’s String Replace Method The string.replace method works roughly how you would expect it to: By default, string.replace will replace all occurrences of the string you want to replace. However, it accepts an optional integer third argument specifying how many times to […]
Regex Cheatsheet
Regex is very powerful and available in virtually every language. Eventually I’ll write a whole post about why every engineer should know some Regex, but this post is all about common regex patterns, flags, etc—in easy-to-read table form. First, here are some patterns similar to ones I’ve used in recent memory: Pattern Meaning Example Match […]
Copy an object in Javascript
There are several ways to copy an object in Javascript, and they fall into two categories: shallow copies and deep copies. Before going deeper, it might be helpful to define the two: If you’re copying a simple Javascript object with top-level properties only, all copies will be deep copies, regardless of what method you use. […]