Arrays and Objects in TypeScript
Learn how to store multiple values in arrays, perform array operations, work with tuples and objects, and extract values using object destructuring in TypeScript.
What Will We Learn?
Arrays and objects are important data structures used in TypeScript applications. They help organize and manage related information efficiently.
- Create arrays and access their elements.
- Use array methods to add, remove, search, filter, and transform elements.
- Understand tuples and their fixed element types.
- Create objects with properties and specified types.
- Extract object properties using destructuring.
Arrays and Objects Workflow
Follow these steps to understand how TypeScript stores and processes data using arrays and objects.
Step 1
Create DataDeclare arrays or objects with suitable types.
Step 2
Store ValuesKeep related information in collections.
Step 3
Process DataAccess, update, or transform values.
Step 4
Use ResultsDisplay or use the processed information.
Visual Example
["Java", "Python", ".NET"]
Ordered collection of values
{
name: "Rahul",
age: 22
}
Access or process stored information
1. Arrays in TypeScript
An array stores multiple values in an ordered collection. TypeScript allows us to specify the data type of its elements.
Example
let courses: string[] = [
"Java",
"Python",
".NET"
];
console.log(courses);
Expected Output
[ 'Java', 'Python', '.NET' ]
Explanation
-
string[]specifies an array of strings. - Each course name is stored as an array element.
- The array can be accessed using indexes starting from 0.
2. Accessing Array Elements
We can access individual array elements using their index positions. The first element has index 0.
Example
let courses: string[] = [
"Java",
"Python",
".NET"
];
console.log(courses[0]);
console.log(courses[1]);
console.log(courses[2]);
Expected Output
Java
Python
.NET
Explanation
-
courses[0]accesses Java. -
courses[1]accesses Python. -
courses[2]accesses .NET.
3. Objects in TypeScript
An object stores related information as properties and values. Each property can have a different type.
Example
let student = {
name: "Rahul",
age: 22,
course: "TypeScript"
};
console.log(student.name);
console.log(student.age);
console.log(student.course);
Expected Output
Rahul
22
TypeScript
Explanation
-
namestores a string. -
agestores a number. -
coursestores the student's course name.
4. Tuples in TypeScript
A tuple is an array type that specifies the expected types of elements at particular positions.
Example
let student: [string, number] = ["Priya", 22];
console.log(student[0]);
console.log(student[1]);
Expected Output
Priya
22
Explanation
- The first element is expected to be a string.
- The second element is expected to be a number.
- Tuple types help describe fixed element positions.
5. Array Methods
TypeScript supports JavaScript array methods for adding, removing, searching, and transforming values.
Example
let courses: string[] = ["Java", "Python"];
courses.push(".NET");
console.log(courses);
courses.pop();
console.log(courses);
Expected Output
[ 'Java', 'Python', '.NET' ]
[ 'Java', 'Python' ]
Explanation
-
push()adds an element to the end. -
pop()removes the last element. -
More methods, including
map(),filter(), andfind(), help process arrays.
6. Object Destructuring
Object destructuring allows us to extract property values from an object into variables.
Example
let student = {
name: "Priya",
age: 22,
course: "Java"
};
let { name, age, course } = student;
console.log(name);
console.log(age);
console.log(course);
Expected Output
Priya
22
Java
Explanation
The destructuring statement extracts the
name, age, and
course properties into separate variables.
This can make code shorter and easier to read.
7. Arrays vs Objects vs Tuples
| Feature | Array | Object | Tuple |
|---|---|---|---|
| Stores | Ordered values | Named properties | Values with positional types |
| Access | Numeric index | Property name | Numeric index |
| Example | string[] |
{ name: string } |
[string, number] |
| Typical use | List of courses | Student details | Name and age in fixed positions |
8. Arrays and Objects Topics
Explore the following topics to learn arrays, tuples, objects, and destructuring step by step.
| Sr. No. | Topic | Description | Action |
|---|---|---|---|
| 1 | Arrays | Create arrays, define types, and access elements. | Learn |
| 2 | Array Methods | Learn push, pop, map, filter, find, and more. | Learn |
| 3 | Tuples | Define element types and positions in tuple values. | Learn |
| 4 | Objects | Work with object properties, values, and types. | Learn |
| 5 | Object Destructuring | Extract object properties into variables. | Learn |
Common Mistakes
- Accessing an array element using an incorrect index.
- Assigning values of an incompatible type to a typed array.
- Confusing object property names with array indexes.
- Using tuple elements in positions that do not match their declared types.
- Trying to destructure a property that does not exist on the object.
Practice Task
Write TypeScript programs to complete the following tasks.
- Create an array of five student names and display each element.
- Use array methods to add, remove, and filter values.
- Create a tuple containing a student's name and age.
- Create a student object containing name, age, course, and marks.
- Use destructuring to extract the student's name and course.
Summary
In this lesson, we introduced arrays, array methods, tuples, objects, and object destructuring. We learned how each structure stores and accesses information, explored practical examples, and reviewed the topics covered in this module.