Collection Operations

Tutorial 3 of 4

Introduction

This tutorial aims to provide a comprehensive understanding of collection operations in JavaScript. We will dive deep into various operations that can be performed on collections, such as adding, removing, and modifying data.

By the end of this tutorial, you will learn:

  • The basics of collections in JavaScript
  • Different operations on collections
  • How to manipulate data in collections

Prerequisites:
To get the most out of this tutorial, you should have a basic understanding of JavaScript syntax and data types.

Step-by-Step Guide

Collections in JavaScript are primarily handled with Arrays and Objects.

Arrays are ordered collections that hold data in a sequence, and Objects are unordered collections where data is stored as key-value pairs.

Some operations that can be performed on collections include:

  1. Adding Data
  2. Removing Data
  3. Modifying Data

To make the most of these operations, remember to keep your code clean and readable, use meaningful variable names, and follow the DRY principle (Don't Repeat Yourself).

Code Examples

Let's now dive into some practical examples:

Adding Data

Example 1: Adding data to an array

let fruits = ['Apple', 'Banana'];
fruits.push('Orange'); // Add 'Orange' to the end of the array

console.log(fruits); // Expected output: ['Apple', 'Banana', 'Orange']

In this example, we use the push() method to add data to the end of an array.

Example 2: Adding data to an object

let car = {
  make: 'Toyota',
  model: 'Corolla'
};

car.year = 2020; // Add a new property 'year' to the object

console.log(car); // Expected output: { make: 'Toyota', model: 'Corolla', year: 2020 }

In this example, we add a new property to the object using dot notation.

Removing Data

Example 3: Removing data from an array

let fruits = ['Apple', 'Banana', 'Orange'];
fruits.pop(); // Remove the last element from the array

console.log(fruits); // Expected output: ['Apple', 'Banana']

In this example, we use the pop() method to remove the last element from the array.

Example 4: Removing data from an object

let car = {
  make: 'Toyota',
  model: 'Corolla',
  year: 2020
};

delete car.year; // Remove the 'year' property from the object

console.log(car); // Expected output: { make: 'Toyota', model: 'Corolla' }

In this example, we remove a property from the object using the delete keyword.

Modifying Data

Example 5: Modifying data in an array

let fruits = ['Apple', 'Banana', 'Orange'];
fruits[1] = 'Blueberry'; // Change the second element of the array

console.log(fruits); // Expected output: ['Apple', 'Blueberry', 'Orange']

In this example, we modify an element in the array by accessing it via its index.

Example 6: Modifying data in an object

let car = {
  make: 'Toyota',
  model: 'Corolla',
  year: 2020
};

car.make = 'Honda'; // Change the 'make' property of the object

console.log(car); // Expected output: { make: 'Honda', model: 'Corolla', year: 2020 }

In this example, we modify a property in the object using dot notation.

Summary

In this tutorial, we covered the basics of collections in JavaScript and how to perform various operations on them, such as adding, removing, and modifying data. The next step would be to learn about more advanced operations, like filtering and sorting data in collections.

Practice Exercises

Exercise 1:

Create an array of your favorite movies and add a new movie to the beginning of the array.

Solution:

let movies = ['Inception', 'Interstellar', 'The Dark Knight'];
movies.unshift('Pulp Fiction'); // Add 'Pulp Fiction' to the beginning of the array

console.log(movies); // Expected output: ['Pulp Fiction', 'Inception', 'Interstellar', 'The Dark Knight']

Exercise 2:

Create an object representing a book and change the book's title.

Solution:

let book = {
  title: 'To Kill a Mockingbird',
  author: 'Harper Lee',
  year: 1960
};

book.title = 'Go Set a Watchman'; // Change the book's title

console.log(book); // Expected output: { title: 'Go Set a Watchman', author: 'Harper Lee', year: 1960 }

Remember to continue practicing and exploring other methods for manipulating collections in JavaScript. Happy coding!