3. Arrays
An array in JavaScript is similar to a Python list: an ordered collection of values of any type that we can add to, delete from, and access by index. They are even created like Python lists:
var badProfessors = []; // empty array
var goodProfessors = ["DeNero", "Hug", "Hilfinger"]; // non-empty array
// we can put anything inside arrays, even other arrays!
var dopeStuff = ["Berkeley", 1, ["public", "university"]];
We can access and assign to individual indices using square brackets:
> var dopeStuff = ["Berkeley", 1, ["public", "university"]];
> dopeStuff[0];
"Berkeley"
> dopeStuff[0] = "UCLA";
> dopeStuff[0];
"UCLA"
Unfortunately (unlike Python), we can't use negative indices. To access the last element of an array, we need to use the length property of an array:
> var dopeStuff = ["Andrew", "is", "GOAT"];
> dopeStuff.length;
3
> dopeStuff[dopeStuff.length - 1];
"GOAT"
3.1 Array Functions
All arrays are technically instances of the special Array
object. This object has some very useful built-in functions.
3.1.1 Array.map
The map function creates a new array with the results of calling a provided function on every element in the calling array. This is a lot like Python list comprehensions, except we are required to provide a function:
> var someNumbers = [1, 4, 5, 9];
> var squaredNumbers = someNumbers.map(function(x) { return x*x; });
> squaredNumbers;
[1, 16, 25, 81]
> someNumbers; // original array is not changed
[1, 4, 5, 9]
3.1.2 Array.forEach
The forEach function is a lot like the map function, but does not create a new array, and does not return anything.
> var someNumbers = [1, 4, 5, 9];
> someNumbers.forEach(function(x) { console.log(x*x); });
1
16
25
81
> someNumbers; // original array is not changed
[1, 4, 5, 9]
3.1.3 Array.push
The push function adds one or more elements to the end of an array (like the list.append function in Python) and returns the new length of the array.
> var someNumbers = [1, 4, 5, 9];
> someNumbers.push(20);
5
> someNumbers;
[1, 4, 5, 9, 20]
3.1.4 Array.pop
The pop function removes the last element from an array and returns that element. This method changes the length of the array. Together with the push function, this is enough to use an array as a stack.
> var someNumbers = [1, 4, 5, 9];
> someNumbers.pop();
9
> someNumbers;
[1, 4, 5]
3.1.5 Array.slice
The slice function returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified. This is similar to Python's list slices.
> var professors = ["DeNero", "Hilfinger", "Hug", "Sahai"];
> var goodProfessors = professors.slice(0, 3);
> goodProfessors;
["DeNero", "Hilfinger", "Hug"]
3.1.6 Array.indexOf
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
> var professors = ["DeNero", "Hug", "Hug", "Sahai"];
> professors.indexOf("Hug");
1
> professors.indexOf("Hilfinger");
-1
3.1.7 Array.join
The join() method joins all elements of an array into a string, with an optional separator
parameter.
> var professors = ["DeNero", "Hilfinger", "Hug", "Sahai"];
> professors.join();
"DeNeroHilfingerHugSahai"
> professors.join(" and ");
"DeNero and Hilfinger and Hug and Sahai"