0% found this document useful (0 votes)
7 views49 pages

Chapter 5 Arrays and Objects

Uploaded by

j5cw7k6222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views49 pages

Chapter 5 Arrays and Objects

Uploaded by

j5cw7k6222
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Chapter 5

JavaScript 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..

What Are Arrays?


Arrays in JavaScript are ordered lists of values. They provide a convenient way to
store multiple items in a single variable. You'll learn about the concept of arrays
and how they differ from other data types.
To work with arrays, you need to know how to create and initialize them. You'll
see how to create arrays using array literals and how to initialize them with initial
values.
Creating and Initializing Arrays
Creating and initializing arrays in JavaScript is a fundamental operation.
Arrays are used to store collections of data, and you can define them in a few
different ways. Here are various methods to create and initialize arrays in
JavaScript:
1. Array Literal:
You can create an array using square brackets with optional initial values
enclosed inside.
Cont…
2. Array Constructor:
You can use the Array constructor to create an array. This constructor can take
zero or more arguments, where each argument becomes an element in the
array..
Cont…
3. Empty Array:
You can create an empty array without any initial values and later populate it
with data
Cont…
4. Array.from():
The Array.from() method is used to create an array from an array-like or iterable
object (e.g., a string).
Cont…
5. Array Initializer:
Use the array initializer syntax to create an array with a specified length. The
elements are initially undefined.
Cont…
6. Spread Operator:
You can create an array by spreading the values from another iterable, like
another array or a string.
Cont…
7. Array.of():
The Array.of() method creates a new array with the given elements as its
elements.

Note:These methods give you flexibility in creating and initializing arrays in


JavaScript.
Choose the one that best suits your needs and programming style.
Accessing Array Elements
Accessing elements in an array in JavaScript is a common operation, and you can
do this using square brackets with the index of the element you want to access.
Remember that JavaScript arrays are zero-indexed, which means the first
element is at index 0, the second element is at index 1, and so on. Here's how
you can access array elements:
Modifying Arrays
Modifying arrays in JavaScript involves adding, updating, or removing elements
from the array. Arrays are mutable, meaning you can change their content after
they've been created. Here are some common operations for modifying arrays:
• Adding Elements
• Updating Elements
• Removing Elements
Cont…
Adding Elements:
• Push Method: You can add an element to the end of an array using the push

method.
Cont…
Adding Elements:
• Unshift Method: You can add an element to the beginning of an array using

the unshift method.


Cont…
Updating Elements:
• You can update an element at a specific position by assigning a new value to

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…

Other Iteration Methods:


JavaScript provides other array methods for specific operations:
• filter(): Creates a new array with elements that pass a test.
• reduce(): Reduces the array to a single value by applying a function.
• every(): Checks if all elements meet a specified condition.
• some(): Checks if at least one element meets a specified condition.
These methods can be used for more specialized iterations, such as filtering
elements or aggregating data.
Cont…

Accessing Index with forEach:


If you need to access both the element and its index, you can do so using the
forEach method:
Multidimensional Arrays

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…

In this example, matrix is a two-dimensional array with three rows and


three columns.
Cont…

Accessing Elements in a Multidimensional Array :


To access elements in a multidimensional array, use multiple sets of square
brackets, with each set representing a different level of nesting. For example, to
access the element in the second row and third column of the matrix:
Cont…

Iterating Through a Multidimensional Array


You can use nested loops to iterate through all elements in a multidimensional
array. For example, to iterate through all elements in the matrix:
Objects
Introduction to Objects
In JavaScript, an object is essentially a collection of key-value pairs, where the
keys are strings (or symbols) and the values can be of various data types,
including other objects. This key-value structure makes objects a powerful and
flexible data structure for organizing and manipulating data.
objects play a central role and are a fundamental concept. JavaScript is a
versatile and widely-used programming language, and it uses objects extensively.
Here's an introduction to objects in JavaScript
Cont…
Creating and Initializing Objects
You can create objects in JavaScript using different methods. The most common
ways are:
• Object Literal Notation.
• Constructor Functions.
• Class Syntax
Cont…
Object Literal Notation:
You can create an object using curly braces {} and specify key-value pairs within
those braces
Cont…
Constructor Functions
You can create objects by defining constructor functions and then using the new
keyword to create instances of those objects.
Cont…
Class Syntax
ES6 introduced class syntax for object-oriented programming. You can create
classes that define the structure of objects and then create instances of those
classes.
Cont…
Accessing Object Properties
You can access object properties using dot notation or square brackets:
Cont…
Adding and Modifying Properties
You can add new properties or modify existing ones in an object
Cont…
Methods in Objects
In addition to properties, objects in JavaScript can contain methods, which are
functions stored as object values. These methods can perform actions related to
the object's data.
Cont…
Object Iteration
You can iterate over an object's properties using for...in loops or Object methods
like Object.keys(), Object.values(), and Object.entries().
Key and Values Result from literal
Object
const person = {
name: 'Amina',
educ_level: 'Bachelor',
gra_status: 'Active'
}

for(let index in person){


console.log(`${index}: ${person[index]}`)
}
JSON
JavaScript Object Notation, commonly referred to as JSON, is a lightweight data
interchange format that is easy for humans to read and write and easy for
machines to parse and generate. JSON is often used for data exchange between
a server and a web application, as well as for configuration files, and for storing
and transmitting structured data.
JSON has become a standard data format in web development, and it has the
following key characteristics:
•Data Structure: JSON represents data as key-value pairs, where keys are strings,
and values can be strings, numbers, objects, arrays, booleans, null, or even other
JSON objects. This simplicity allows for nesting and structuring data in a
hierarchy.
JSON
•Syntax: JSON has a simple and readable syntax, which is similar to JavaScript
object literal notation. Here's an example of JSON data:
JSON
Data Types:
• Strings are enclosed in double quotes: “Mohamed Ali".
• Numbers can be integers or floating-point values: 30 or 3.14.
• Objects are enclosed in curly braces: {"name": " Mohamed Ali "}.
• Arrays are enclosed in square brackets: ["apple", "banana", “mango"].
• Booleans can be true or false.
• null represents a null or empty value.
JSON
•Nesting: JSON allows for the nesting of objects and arrays within each other,
creating complex data structures.
JSON
No Functions: JSON does not support functions or methods, making it purely a
data format and not a programming language.
Lightweight: JSON is minimal and lightweight, making it efficient for data
transmission over the network.
Widely Supported: JSON is supported by many programming languages, not just
JavaScript. It has parsers and serializers available for most modern programming
environments.
Human-Readable: JSON is easy for humans to read and write, which makes it a
popular choice for configuration files and data interchange.
JSON
To work with JSON in JavaScript, you can use the JSON object, which provides
methods for parsing JSON strings into JavaScript objects (JSON.parse()) and
converting JavaScript objects into JSON strings (JSON.stringify()).
Here's an example of parsing JSON
JSON
And here's an example of converting a JavaScript object to a JSON string:

JSON's simplicity and compatibility with a wide range of programming languages


make it a popular choice for data interchange and storage in web applications
and various other software systems.
END

49

You might also like