How to Find an Object in an Array in TypeScript

Find an object in an array in TypeScript: Command Prompt output of tsc and node finding a product object by id and name with Array.find

To find an object in an array in TypeScript, use the array find() method with a condition on a property: products.find(p => p.id === 102) returns the first matching object, or undefined if there is none. TypeScript types the result as Product | undefined, so you must handle the missing case. This guide covers TypeScript … Read more >>

Convert a String to Lowercase in TypeScript

Python replace multiple characters in a string

As a TypeScript developer, you might be working with string manipulations. One such example is converting strings to lowercase. TypeScript provides several methods to convert strings to lowercase. In this tutorial, I’ll walk you through various methods to convert strings to lowercase in TypeScript, complete with examples and best practices. Method 1: Using the toLowerCase() … Read more >>

Get and Check String Length in TypeScript

Get and Check String Length in TypeScript

When building a web application using TypeScript, checking the length of user input is a common and important task. For example, on a user registration form for an e-commerce website, you may want to ensure that the username is at least 5 characters long, the password has a minimum of 8 characters, and the description … Read more >>

How to Reverse a String in TypeScript?

Reverse String in TypeScript

In one of my recent TypeScript applications, I had to reverse a string where we were generating custom tracking codes. Each code had to be displayed in reverse for security reasons before sending it to the frontend. While searching for solutions, I came across different methods to reverse a string in TypeScript, such as split, … Read more >>

How to Check if a String Is Empty in TypeScript

Check if a string is empty in TypeScript: Command Prompt output of isNullOrEmpty and isNullOrWhitespace functions on empty, blank, null and undefined values

To check if a string is empty in TypeScript, compare it with an empty string: str === “” (or str.length === 0). If the value can also be null or undefined, use str == null || str === “”, and add trim() when a string of spaces should count as empty. This guide covers each … Read more >>

Check If a Key Exists in a TypeScript Object

Check If a Key Exists in a TypeScript Object

While working on a TypeScript project, you receive data from an API as an object. Before using any property from that object, you want to make sure the key actually exists to avoid errors. For example, you want to display the user’s email, but you’re not sure if the email key is present in the … Read more >>

How to Check if an Object Is Empty in TypeScript

Check if an object is empty in TypeScript: Command Prompt output of an isEmptyObject type guard tested with objects, null, arrays, Date and Map

To check if an object is empty in TypeScript, count its own keys: Object.keys(obj).length === 0 is true for {} and false as soon as the object has a property. In real code you also need to handle null and undefined, and decide what to do with arrays, dates and class instances. This guide builds … Read more >>

How to Sort a Number Array in Descending Order using TypeScript?

Sorting Numbers in Descending Order in TypeScript

Recently, one of my team members asked me to sort a numeric array in descending order. In this tutorial, I will explain how to sort a number array in descending order using TypeScript, using different methods and examples. Understanding the sort() Method TypeScript, being a superset of JavaScript, inherits the sort() method for arrays. By default, the … Read more >>

How to Sort an Array by Date in TypeScript?

In this tutorial, I will explain how to sort an array by date in TypeScript. Sorting arrays by date is a common task in web development, especially when dealing with data such as event schedules, appointment bookings, or historical records. I will cover both ascending and descending order sorting an array in TypeScript and include … Read more >>

How to Sort an Array of Objects by Property in TypeScript

Sort an array of objects by property in TypeScript: Command Prompt output of tsc and node sorting employees by age and by name

To sort an array of objects by property in TypeScript, pass a compare function to sort(): arr.sort((a, b) => a.age – b.age) for a number property and arr.sort((a, b) => a.name.localeCompare(b.name)) for a string property. Swap a and b for descending order. This guide covers the TypeScript array sort for numbers, strings and dates, sorting … Read more >>