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:
Prerequisites:
To get the most out of this tutorial, you should have a basic understanding of JavaScript syntax and data types.
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:
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).
Let's now dive into some practical examples:
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.
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.
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.
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.
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!