Chapter 7: Arrays Flashcards
(45 cards)
Ways to create array
Array literals
The … spread operator on an iterable object
The Array() constructor
The Array.of() and Array.from() factory methods
By far the simplest way to create an array is with an _____ _____, which
is simply a comma-separated list of array elements within square
brackets
array literal
Array literals can contain ______ literals or other array literals
object
let b = [[1, {x: 1, y: 2}], [2, {x: 3, y: 4}]];
Spread operator
let a = [1, 2, 3];
let b = [0, …a, 4]; // b == [0, 1, 2, 3, 4]
Array constructor with no arguments
let a = new Array();
Array constructor with specified length
let a = new Array(10);
Array constructor with specified elements
let a = new Array(5, 4, 3, 2, 1,”testing, testing”);
Aray.of()
Array.of() // => []; returns empty array with no
arguments
Array.of(10) // => [10]; can create arrays with a single
numeric argument
Array.of(1,2,3) // => [1, 2, 3]
______ is another array factory method introduced in ES6. It
expects an iterable or array-like object as its first argument and returns
a new array that contains the elements of that object. W
Array.from()
Example of reading and writing from arrays
let a = [“world”]; // Start with a one-element array
let value = a[0]; // Read element 0
a[1] = 3.14; // Write element 1
let i = 2;
a[i] = 3; // Write element 2
a[i + 1] = “hello”; // Write element 3
a[a[i]] = a[0]; // Read elements 0 and 2, write
element 3
How to get leth of array a
a.length
A _____ _____ is one in which the elements do not have contiguous
indexes starting at 0
sparse array
Example of spare array
let a = new Array(5); // No elements, but a.length is 5.
a = []; // Create an array with no elements and
length = 0.
a[1000] = 0; // Assignment adds one element but sets
length to 1001.
When an array is sparse, the _____ property is greater than the
number of elements
length
Adding elements to array
let a = []; // Start with an empty array.
a[0] = “zero”; // And add elements to it.
a[1] = “one”;
You can also use the ____ method to add one or more values to the
end of an array:
push()
You can delete array elements with the ______ operator, just as you
can delete object properties
delete
let a = [1,2,3];
delete a[2]; // a now has no element at index 2
2 in a // => false: no array index 2 is defined
a.length // => 3: delete does not affect array length
Deleting an array element is similar to (but subtly different than)
assigning _______ to that elemen
undefined
Note that using delete on an
array element does not alter the _____ property and does not shift
elements with higher indexes down to fill in the gap that is lef
length
If you delte an element from array, the array becomes _____
sparse
Iterating through an array
let letters = […“Hello world”]; // An array of letters
let string = “”;
for(let letter of letters) {
string += letter;
}
string // => “Hello world”; we reassembled the original text
for-each syntax
let uppercase = “”;
letters.forEach(letter => { // Note arrow function syntax
here
uppercase += letter.toUpperCase();
});
uppercase // => “HELLO WORLD”
Regular for loop in javascript
// Save the array length into a local variable
for(let i = 0, len = letters.length; i < len; i++) {
// loop body remains the same
}
// Iterate backwards from the end of the array to the start
for(let i = letters.length-1; i >= 0; i–) {
// loop body remains the same
}
JavaScript does not support true multidimensional arrays, but you can
approximate them with arrays of arrays. To access a value in an array
of arrays, simply use the ___ operator twice
[]