Remove Duplicates from an Array of Objects in TypeScript

Remove duplicates from an array of objects in TypeScript: Command Prompt output of four orders reduced to three unique rows by id

To remove duplicates from an array of objects in TypeScript, key them with a Map: […new Map(orders.map(o => [o.id, o])).values()] gives you one row per id, in the order they first appeared. A Set will not do it, because it compares objects by reference rather than by contents. This guide covers deduplicating by one property, … Read more >>

How to Sort Arrays in TypeScript

Sort arrays in TypeScript: Command Prompt output of sort() with and without a compare function on numbers and strings

To sort an array in TypeScript, call sort() with a compare function: numbers.sort((a, b) => a – b) for numbers and names.sort((a, b) => a.localeCompare(b)) for text. Swap a and b for descending order. Without a compare function sort() turns every element into a string first, which is why [100, 25, 9, 1000, 3] comes … Read more >>

Check if a TypeScript Array Contains a Specific Value

Check if a TypeScript array contains a specific value: Command Prompt output of includes() on an array of cities

To check if a TypeScript array contains a specific value, call includes(): cities.includes(“Denver”) returns true or false. That works for strings, numbers and booleans. For an array of objects it will not, because includes() compares by reference, so you test a property with some() instead: orders.some(o => o.id === “ORD-1001”). This guide covers both, plus … Read more >>

Remove Empty Strings from an Array in JavaScript and TypeScript

Remove empty strings from an array: Command Prompt output of filter(Boolean) and an explicit filter on an array of city names

To remove empty strings from an array, filter them out: values.filter(Boolean) is the short version and values.filter(value => value !== “”) is the explicit one. Both return a new array and leave the original alone. If the array also contains strings that are only spaces or tabs, trim first: values.filter(value => value.trim() !== “”). The … Read more >>

How to Get Distinct Values from an Array in TypeScript

TypeScript distinct values: Command Prompt output of new Set removing repeated state codes from an array

To get distinct values from an array in TypeScript, pass the array to a Set and spread it back: […new Set(arr)]. A Set keeps each value once, in first-seen order, and TypeScript keeps the element type, so a string[] stays a string[]: I compiled these snippets with TypeScript 7.0.2 running on Node.js v22.22.2. One of … Read more >>

How to Remove Multiple Keys from a Dictionary in Python

Python remove multiple keys from dictionary: Command Prompt output of a pop loop removing private fields

To remove multiple keys from a dictionary in Python, loop over the keys and call d.pop(key, None) for each one. The None default means missing keys are skipped instead of raising KeyError. To keep the original intact, build a new dict with a comprehension: The examples strip private fields from US customer and order records, … Read more >>

How to Split an Array in TypeScript

TypeScript split array: Command Prompt output of a generic chunk function splitting emails into batches

To split an array in TypeScript, use slice(): arr.slice(0, i) gives the part before index i and arr.slice(i) gives the rest, without changing the original. To split into chunks of N items, call slice in a loop: For the outputs on this page I used TypeScript 7.0.2 on Node v22.22.2 with strict checks, so the … Read more >>

How to Write Bytes to a File in Python

Python write bytes to file: Command Prompt output of writing encoded invoice bytes in wb mode and reading them back

To write bytes to a file in Python, open it in binary write mode with open(path, “wb”) and call f.write(data), where data is a bytes or bytearray object. For a one-liner, Path(path).write_bytes(data) does the same: Binary mode works the same on every OS, but I ran these on Windows with Python 3.12.5, where text-mode newline … Read more >>

Python break and continue: Exit or Skip a Loop

Python break and continue: Command Prompt output of a for loop skipping cancelled orders with continue

Python’s break and continue both change how a loop runs. break exits the loop immediately, and continue skips the rest of the current pass and moves on to the next one. Both work in for and while loops. The orders, statuses and seat numbers below are made up, but every output is real: I ran … Read more >>

How to pip install a Specific Version of a Package in Python

pip install a specific version: Command Prompt showing pip installing tabulate 0.9.0 and Python confirming the installed version

pip installs a specific version when you put == and the version number after the package name. The same command downgrades or upgrades whatever’s already installed: I tested every command below in a real Windows Command Prompt with Python 3.12.5 and pip 24.2, inside a separate environment so you can see each install happen from … Read more >>