Welcome to today's lesson! Our topic for the day is data aggregation, a crucial aspect of data analysis. Like summarizing a massive book into key points, data aggregation summarizes large amounts of data into important highlights.
By the end of this lesson, you'll be equipped with several aggregation methods to summarize data streams in TypeScript. Let's get started!
Let's say we have an array of numbers denoting the ages of a group of people:
Common questions we might ask are: How many people are in the group? What's their total age? Who's the youngest and the oldest? TypeScript's type annotations, combined with methods like length
, reduce
, Math.min
, and Math.max
, have our answers:
In this code snippet, we utilize several built-in functions for basic aggregation tasks. The length
property is used to count the number of elements in the array, giving us the total number of people. The function used here to sum the ages, will be discussed in detail later in this lesson. For now, it is important to note that accumulates elements of an array into a single value. Next, and help determine the youngest and oldest ages by finding the minimum and maximum values in the array, respectively. Finally, the average age is calculated by dividing the total age by the number of people, and the age range is obtained by subtracting the youngest age from the oldest age. These operations, along with TypeScript’s type annotations, ensure both code clarity and type safety.
