Chapter 7: Arrays Flashcards

(45 cards)

1
Q

Ways to create array

A

Array literals
The … spread operator on an iterable object
The Array() constructor
The Array.of() and Array.from() factory methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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

A

array literal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Array literals can contain ______ literals or other array literals

A

object
let b = [[1, {x: 1, y: 2}], [2, {x: 3, y: 4}]];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Spread operator

A

let a = [1, 2, 3];
let b = [0, …a, 4]; // b == [0, 1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Array constructor with no arguments

A

let a = new Array();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Array constructor with specified length

A

let a = new Array(10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array constructor with specified elements

A

let a = new Array(5, 4, 3, 2, 1,”testing, testing”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Aray.of()

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

______ 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

A

Array.from()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example of reading and writing from arrays

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to get leth of array a

A

a.length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A _____ _____ is one in which the elements do not have contiguous
indexes starting at 0

A

sparse array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Example of spare array

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When an array is sparse, the _____ property is greater than the
number of elements

A

length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Adding elements to array

A

let a = []; // Start with an empty array.
a[0] = “zero”; // And add elements to it.
a[1] = “one”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

You can also use the ____ method to add one or more values to the
end of an array:

A

push()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

You can delete array elements with the ______ operator, just as you
can delete object properties

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Deleting an array element is similar to (but subtly different than)
assigning _______ to that elemen

A

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

20
Q

If you delte an element from array, the array becomes _____

21
Q

Iterating through an array

A

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

22
Q

for-each syntax

A

let uppercase = “”;
letters.forEach(letter => { // Note arrow function syntax
here
uppercase += letter.toUpperCase();
});
uppercase // => “HELLO WORLD”

23
Q

Regular for loop in javascript

A

// 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
}

24
Q

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

25
The _____ method iterates through an array, invoking a function you specify for each element. As we’ve described, you pass the _____ as the first argument to ______ (same as first blank)
forEach(), function
26
Example of forEach()
let data = [1,2,3,4,5], sum = 0; // Compute the sum of the elements of the array data.forEach(value => { sum += value; });
27
The _____ method passes each element of the array on which it is invoked to the function you specify and returns an array containing the values returned by your function
map() let a = [1, 2, 3]; a.map(x => x*x) // => [1, 4, 9]: the function takes input x and returns x*x
28
Note that map() returns a new array: it does not ____ the array it is invoked on
MODIFY
29
The _____ method returns an array containing a subset of the elements of the array on which it is invoked. The function you pass to it should be predicate: a function that returns true or false
filter() let a = [5, 4, 3, 2, 1]; a.filter(x => x < 3) // => [2, 1]; values less than 3 a.filter((x,i) => i%2 === 0) // => [5, 3, 1]; every other value
30
The _____ and __________ methods are like filter() in that they iterate through your array looking for elements for which your predicate function returns a truthy value
find(), findIndex() let a = [1,2,3,4,5]; a.findIndex(x => x === 3) // => 2; the value 3 appears at index 2
31
The ______ and ______ methods are array predicates: they apply a predicate function you specify to the elements of the array, then return true or false
every(), some() let a = [1,2,3,4,5]; a.every(x => x < 10) // => true: all values are < 10. a.every(x => x % 2 === 0) // => false: not all values are even.
32
The ______ and _______ methods combine the elements of an array, using the function you specify, to produce a single value
reduce(), reduceRight()
33
____ method creates and returns a new array that contains the same elements as the array it is called on, except that any elements that are themselves arrays are “flattened” into the returned
flat() [1, [2, 3]].flat() // => [1, 2, 3] [1, [2, [3]]].flat() // => [1, 2, [3]]
34
flatMap() example
// Map non-negative numbers to their square roots [-2, -1, 1, 2].flatMap(x => x < 0 ? [] : Math.sqrt(x)) // => [1, 2**0.5]
35
The ______ method creates and returns a new array that contains the elements of the original array on which concat() was invoked, followed by each of the arguments to ______
concat()
36
The _____ and _____ methods allow you to work with arrays as if they were stacks
push(), pop()
37
The _____ and _____ methods behave much like push() and pop(), except that they insert and remove elements from the beginning of an array rather than from the end
unshift(),shift() let q = []; // q == [] q.push(1,2); // q == [1,2] q.shift(); // q == [2]; returns 1 q.push(3) // q == [2, 3] q.shift() // q == [3]; returns 2 q.shift() // q == []; returns 3
38
When passing multiple arguments to unshift(), they are inserted all at once, which means that they end up in the array in a _____ order than they would be if you inserted them one at a time
different
39
slice() method for extraction
let a = [1,2,3,4,5]; a.slice(0,3); // Returns [1,2,3] a.slice(3); // Returns [4,5] a.slice(1,-1); // Returns [2,3,4] a.slice(-3,-2); // Returns [3]
40
______ is a general-purpose method for inserting or removing elements from an array. Unlike slice() and concat(), splice() modifies the array on which it is invoked
splice()
41
The _____ method sets the elements of an array, or a slice of an array, to a specified value. It mutates the array it is called on, and also returns the modified array
fill()
42
_______ and _______ search an array for an element with a specified value and return the index of the first such element found, or -1 if none is found
indexOf(), lastIndexOf()
43
The ES2016 ______ method takes a single argument and returns true if the array contains that value or false otherwise
includes()
44
_____ sorts the elements of an array in place and returns the sorted array. When sort() is called with no arguments, it sorts the array elements in alphabetical order
sort()
45
The ______ method reverses the order of the elements of an array and returns the reversed array. It does this in place; in other words, it doesn’t create a new array with the elements rearranged but instead rearranges them in the already existing array
reverse()