Javascript Arrays
Javascript Arrays
Javascript Arrays
1
0
JavaScript: Arrays
OBJECTIVES
In this chapter you will learn:
To use arrays to store lists and tables of
values.
To declare an array, initialize an array and
refer to individual elements of an array.
To pass arrays to functions.
To search and sort an array.
To declare and manipulate
multidimensional arrays.
10.1
Introduction
10.2
Arrays
10.3
10.4
10.5
10.6
10.7
10.8
Sorting Arrays
10.9
10.10
Multidimensional Arrays
10.11
10.12
Wrap-Up
10.13
Web Resources
10.1 Introduction
Arrays
Data structures consisting of related data items
Sometimes called collections of data items
JavaScript arrays
dynamic entities that can change size after they are
created
10.2 Arrays
An array is a group of memory locations
All have the same name and normally are of the same type
(although this attribute is not required in JavaScript)
10
Fig. 10.2 |
Precedence and
associativity of
the operators
discussed so
far.
11
12
13
Fig. 10.3 |
Initializing the
elements of an
array (Part 1 of
3).
14
Fig. 10.3 |
Initializing the
elements of an
array (Part 2 of
3).
15
16
17
18
19
20
21
Fig. 10.4 |
Declaring and
initializing
arrays (Part 1 of
3).
22
Fig. 10.4 |
Declaring and
initializing
arrays (Part 2 of
3).
23
24
Fig. 10.5 |
Summing
elements of an
array (Part 1 of
2).
25
Fig. 10.5 |
Summing
elements of an
array (Part 2 of
2).
26
27
28
switch (Part 1 of
2).
Creates a frequency array with
each elements index
corresponding to a face value (we
leave index 0 uninitialized because
the lowest face value is 1)
29
switch (Part 2 of
2).
30
31
Fig. 10.7 |
Random image
generation
using arrays
(Part 1 of 2).
Creates an array with the names
of the images to choose from
32
33
pass-by-value
pass-by-reference
Pass-by-value
a copy of the arguments value is made and is passed to the called function
The caller gives the called function direct access to the callers data and allows it to modify
the data if it so chooses
Can improve performance because it can eliminate the overhead of copying large amounts of
data, but it can weaken security because the called function can access the callers data
34
Name of an array
actually a reference to an object that contains the array elements and
the length variable
35
36
37
38
Specify the name of the array (a reference to the array) without brackets
Such simple single pieces of data are called scalars, or scalar quantities
To pass an array element to a function, use the subscripted name of the element as an
argument in the function call
Returns a string that contains all of the elements of an array, separated by the string
supplied in the functions argument
If an argument is not specified, the empty string is used as the separator
Fig. 10.8 |
Passing arrays
and individual
array elements
to functions
(Part 1 of 3).
39
Fig. 10.8 |
Passing arrays
and individual
array elements
to functions
(Part 2 of 3).
Creates a string
containing all the
elements in
theArray,
separated by
40
41
Fig. 10.8 | Passing arrays and individual array elements to functions (Part 3 of 3).
42
43
With no arguments, the method uses string comparisons to determine the sorting order of
the Array elements
Method sort takes as its optional argument the name of a function (called the comparator
function) that compares its two arguments and returns a negative value, zero, or a positive
value, if the first argument is less than, equal to, or greater than the second, respectively
They can be assigned to variables, stored in Arrays and passed to functions just like other
data
44
Fig. 10.9 |
Sorting an array
with sort (Part
1 of 2).
Passes function
compareIntegers to method
a.sort to arrange the elements
of a in ascending numerical
order
Defines a function
comparing integers
to be passed to
method sort (to
replace the default
string comparison
function)
45
Fig. 10.9 |
Sorting an array
with sort (Part
2 of 2).
46
47
48
Fig. 10.10 |
Linear search of
an array (Part 1
of 3).
Creates a new array to search
49
Fig. 10.10 |
Linear search of
an array (Part 2
of 3).
Calls function linearSearch on
array a with the value input by the user
50
Fig. 10.10 |
Linear search of
an array (Part 3
of 3).
When the Search button is
pressed, calls function
buttonPressed
51
For a one-billion-element array, this is the difference between an average of 500 million
comparisons and a maximum of 30 comparisons
52
Fig. 10.11 |
Binary search of
an array (Part 1
of 5).
53
Fig. 10.11 |
Binary search of
an array (Part 2
of 5).
Calls function binarySearch with
arguments a and the key
specified by the user
54
Binary search of
an array (Part 3
Otherwise, if of
the5).
middle
elements value is higher than
the key, we only need to search
the bottom half of the array
55
Fig. 10.11 |
Binary search of
an array (Part 4
of 5).
56
57
58
Fig. 10.12 | Two-dimensional array with three rows and four columns.
59
60
Fig. 10.13 |
Initializing
multidimensional
arrays (Part 1 of
2).
Initializes array1 with an
initializer list of sub initializer lists
Initializes array2 with rows of
different lengths
61
Fig. 10.13 |
Initializing
multidimensional
arrays (Part 2 of
2).
62
63
Fig. 10.14 |
Online quiz
graded with
JavaScript
(Part 1 of 3).
myQuiz.elements[0]
64
myQuiz.elements[1]
Fig. 10.14 |
Online quiz
graded with
JavaScript
(Part 2 of 3).
myQuiz.elements[2]
myQuiz.elements[3]
65