Chapter 5 Arrays and Objects
Chapter 5 Arrays and Objects
1
Objectives
Understand what arrays are and their purpose in JavaScript.
Differentiate between arrays and other data types.
Create arrays using various methods (array literals, constructor, and
Array.from()).
Initialize arrays with elements of different data types.
Learn how to access elements in an array using indexing.
Explore array indexing, including zero-based indexing and negative indexing.
Study common array methods such as push, pop, shift, unshift, concat, and
splice.
Understand how to modify arrays using these methods.
Use for loops, forEach, map, and other methods to iterate through arrays.
Objectives
Perform various operations on array elements during iteration.
Understand multidimensional arrays and the concept of arrays of arrays.
Understand the key-value pair concept in objects.
Learn how objects are used to represent structured data.
Create objects using object literals and the constructor method.
Initialize objects with properties and values.
Access object properties using dot notation and bracket notation.
Learn about computed property names for dynamic property access.
Define methods within objects and Explore methods for manipulating and
interacting with object data.
Add, update, and delete properties from objects.
Introduction to Arrays
In this chapter, we'll dive deep into two essential data structures in JavaScript:
arrays and objects. These data structures are used for storing and managing
data in your JavaScript programs..
method.
Cont…
Adding Elements:
• Unshift Method: You can add an element to the beginning of an array using
that index.
Cont…
Removing Elements :
• Pop Method: You can remove the last element of an array using the pop
method.
Cont…
Removing Elements :
• Shift Method: You can remove the first element of an array using the shift
method.
Cont…
Removing Elements :
• Splice Method: The splice method allows you to remove elements from an
array by specifying the starting index and the number of elements to remove.
Cont…
Removing Elements :
• It's important to note that these array modification methods can also return
the elements they remove, which can be useful for various purposes.
• Modifying arrays is a common task when working with data, and these
methods allow you to manipulate the contents of an array according to your
needs.
Iterating Through Arrays
Iterating through arrays in JavaScript is a fundamental operation that allows you
to access and process each element in the array.
There are several methods and techniques for iterating through arrays:
• For Loop
• forEach() Method
• for...of Loop
• map() Method
• Accessing Index with forEach
Cont…
For Loop
A for loop is a traditional way to iterate through an array, and it gives you fine
grained control over the iteration.
Cont…
forEach() Method
The forEach() method is a more concise way to iterate through an array and is
especially useful for executing a function on each element.
Cont…
for...of Loop
The for...of loop is a modern and cleaner alternative to a traditional for loop for
iterating through arrays.
Cont…
map() Method
The map() method creates a new array by applying a provided function to each
element of the original array.
Cont…
Multidimensional arrays are arrays within arrays, allowing you to create complex
data structures in JavaScript. These arrays are commonly used to represent
tables, matrices, and nested data. Here's how to work with multidimensional
arrays in JavaScript
Creating a Multidimensional Array:
To create a multidimensional array, you simply nest one or more arrays within an
array. Here's an example of a two-dimensional array (an array of arrays):
Cont…
49