Mozilla "Array" Flashcards

1
Q

The ________ method creates a new, shallow-copied Array instance from an array-like or iterable object

A

Array.from()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
console.log((\_\_\_\_\_\_\_\_\_\_'foo'));
// expected output: Array ["f", "o", "o"]
console.log(\_\_\_\_\_\_\_\_\_\_\_([1, 2, 3], x => x * x));
// expected output: Array [2, 4, 6]
A

Array.from

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

The __________ method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

A

concat()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.log(array1.\_\_\_\_\_\_\_\_(array2));
// expected output: Array ["a", "b", "c", "d", "e", "f"]
A

concat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_);
// expected output: Array ["a", "b", "c", "d", "e", "f"]
A

array1.concat(array2)

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

The_______method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments.

A

Array.of()

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

The ___________ method determines whether the passed value is an Array.

A

Array.isArray()

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

Array.of(7); //

A

[7]

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

Array(7) //

A

[ , , , , , , ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
console.log(Array.\_\_\_\_\_([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]
A

from

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

The _________ method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

A

join()

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

var elements = [‘Fire’, ‘Air’, ‘Water’];

console.log(elements.join());
// expected output: 
console.log(elements.join('-'));
// expected output:
A

“Fire,Air,Water”

“Fire-Air-Water”

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

var elements = [‘Fire’, ‘Air’, ‘Water’];

console.log(\_\_\_\_\_\_\_\_\_\_\_\_);
// expected output: "Fire-Air-Water"
A

elements.join(‘-‘)

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

The ________ method returns a new Array Iterator object that contains the keys for each index in the array.

A

keys()

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

The __________ method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

A

reverse()

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

The _______ method creates and returns a new string, the ______ method a array

A

join()

concat()

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

The ____________ method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

A

every()

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

The every() returns a _________

A

Boolean value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
function isBelowThreshold(currentValue) {
  return currentValue < 40;
}

var array1 = [1, 30, 39, 29, 10, 13];

console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_);
// expected output: true
A

array1.every(isBelowThreshold)

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

Array.of(7); //

Array.of(1, 2, 3); //

A

[7]

[1, 2, 3]

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

The ________ method sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

A

sort()

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

The _________ method returns a string representing the specified array and its elements.

A

toString()

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

var array1 = [1, 2, ‘a’, ‘1a’];

console.log(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_);
// expected output: "1,2,a,1a"
A

array1. toString()

array1. join()

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

The __________ method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

A

includes()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
includes() returns _________?
true or false
26
2 ways of creating array let arr = let arr =
``` new Array(); []; ```
27
``` let fruits = ["Apple", "Orange", "Plum"]; fruits[2] = 'Pear'; // ```
now ["Apple", "Orange", "Pear"]
28
``` let fruits = ["Apple", "Orange", "Plum"]; fruits[3] = 'Lemon'; ```
// now ["Apple", "Orange", "Pear", "Lemon"]
29
let fruits = ["Banana"] let arr = fruits; alert( arr === fruits ); //
true
30
let fruits = ["Apple", "Orange", "Plum"]; // iterates over array elements USING FOR OF
for (let fruit of fruits) { alert( fruit ); }
31
let arr = [1, 2, 3, 4, 5]; arr.length = 2; alert( arr ); // [1, 2] arr.length = 5; // return length back alert( arr[3] ); // ??
undefined: the values do not return
32
simplest way to clear the array is _______
arr.length = 0;
33
let arr = new Array(2) alert( arr[0] ); //
undefined! no elements.
34
let arr = new Array(2); // will it create an array of [2] ? alert( arr.length ); //
length 2
35
let arr = ["I", "go", "home"]; delete arr[1]; alert( arr[1] ); // alert( arr.length ); //
undefined 3
36
The _________ method allows to run a function for every element of the array.
forEach
37
``` arr.forEach(function(____ , ________ , _______) { // ... do something with item }); ```
item, index, array
38
3 search methods for array
indexOf, lastIndexOf includes
39
let arr = [1, 0, false]; alert( arr.indexOf(0) ); // alert( arr.indexOf(false) ); // alert( arr.indexOf(null) ); // alert( arr.includes(1) ); //
1 2 -1 true
40
let result = arr.find(function(__ , ______ , ______) { }); returns
item, index, array
41
``` let users = [ {id: 1, name: "John"}, {id: 2, name: "Pete"}, {id: 3, name: "Mary"} ]; ``` let user = users.find(item => item.id == 2); alert(user.name); //
Pete
42
``` let users = [ {id: 1, name: "John"}, {id: 2, name: "Pete"}, {id: 3, name: "Mary"} ]; ``` let user = users.find(_____________); alert(user.name); // John
item => item.id == 1
43
``` var myA1 = [1,2,3]; var myA2 = [1,2,3]; ``` console.log(myA1==myA2);
false
44
let arr = [ 1, 2, 15 ]; ``` // the method reorders the content of arr (and returns it) arr.sort(); ``` alert( arr ); //
1, 15, 2
45
let arr = [ 1, 2, 15, 155 ]; arr.sort(); alert( arr );
1, 15, 155, 2
46
"2" > "15"
TRUE
47
``` function compareNumeric(a, b) { if (a > b) return if (a == b) return if (a < b) return } ```
1 0 -1
48
let str = "test"; alert(________); // t,e,s,t
str.split('')
49
When we need to iterate over an array – we can use _____________________ name all 3
forEach, for or for..of.
50
The ____________ method executes a provided function once for each array element.
forEach()
51
_________— executes a provided function once for each array element. __________— creates a new array with the results of calling a provided function on every element in the calling array.
forEach()  map() 
52
The ________method shallow copies part of an array to another location in the same array and returns it without modifying its length.
copyWithin()
53
copyWithin()
The copyWithin() method shallow copies part of an array to another location in the same array and returns it without modifying its length. arr.copyWithin(target[, start[, end]])
54
var array1 = ['a', 'b', 'c', 'd', 'e']; ``` console.log(array1.copyWithin(0, 3, 4)); // expected output: ```
Array ["d", "b", "c", "d", "e"]
55
var array1 = ['a', 'b', 'c', 'd', 'e']; ``` console.log(array1.copyWithin(1, 3)); // expected output: ```
Array ["d", "d", "e", "d", "e"]
56
arr.copyWithin(________)
target[, start[, end]]
57
The ___________ method returns the first index at which a given element can be found in the array, or -1 if it is not present.
indexOf()
58
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; ``` console.log(beasts.indexOf('bison')); // expected output: ```
1
59
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; ``` // start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: ```
4
60
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; ``` console.log(beasts.indexOf('giraffe')); // expected output: ```
// expected output: -1
61
arr.indexOf(_________[,__________])
searchElement fromIndex
62
var array1 = [1, 2, 3]; ``` console.log(array1); // expected output: ```
Array [1, 2, 3]
63
arr.copyWithin(______[,________[, ______]])
target . start . end
64
USE .some or .every? ``` const userPrivileges = ['user', 'user', 'user', 'admin']; const containsAdmin = userPrivileges._______( element => element === 'admin'); // containsAdmin will be equal to true ```
some
65
use .some or .every? ``` const ratings = [3, 5, 4, 3, 5]; const goodOverallRating = ratings.______( rating => rating >= 3 ); // goodOverallRating will be equal to true ```
every
66
``` const icecreamColors = { chocolate: 'brown', vanilla: 'white', strawberry: 'red', } ``` ``` const colors =__________; // colors will be equal to ["brown", "white", "red"] ```
Object.values(icecreamColors)
67
``` const icecreamColors = { chocolate: 'brown', vanilla: 'white', strawberry: 'red', } ``` ``` const types = _________ // types will be equal to ["chocolate", "vanilla", "strawberry"] ```
Object.keys(icecreamColors);
68
``` var arr = [[1,2], [3,4], [5,6]]; alert the number 5 ```
alert(arr[2][0]);
69
The _________ method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.
some()
70
The _________ method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
lastIndexOf()
71
``` var animals = ['Dodo', 'Tiger', 'Penguin']; console.log(animals.lastIndexOf('Penguin')); // expected output: ``` ``` console.log(animals.lastIndexOf('Dodo')); // expected output: ```
2 0
72
The___________ method fills (modifies) all the elements of an array from a start index (default zero) to an end index (default array length) with a static value. It returns the modified array.
fill()
73
var array1 = [1, 2, 3, 4]; array1.fill(6) returns
// expected output: [6, 6, 6, 6]
74
var array1 = [1, 2, 3, 4]; console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0] // fill with 0 from position 2 until position 4
75
``` var array1 = [1, 2, 3, 4]; console.log(array1.fill(5, 1)); ```
// expected output: [1, 5, 5, 5]
76
var array = [2, 9, 9]; array.indexOf(7); //
-1
77
var array = [2, 9, 9]; array.indexOf(9, 2); //
2
78
``` var array = [2, 9, 9]; array.indexOf(2, -1); // ```
-1
79
``` var array = [2, 9, 9]; array.indexOf(2, -3); // ```
0
80
__________syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Spread operator
81
``` function sum(x, y, z) { return x + y + z; } ``` const numbers = [1, 2, 3]; ``` console.log(sum(...numbers)); // expected output: ```
6
82
``` function sum(x, y, z) { return x + y + z; } ``` const numbers = [1, 2, 3]; ``` console.log(sum.apply(null, numbers)); // expected output: ```
6
83
Concating these two arrays will result// my arr1 = [["steve", 33],["me", 22]] my arr2 = ["john", 55]
[["steve", 33],["me", 22], "john", 55]
84
Concating these two arrays will result// my arr1 = [["steve", 33],["me", 22]] my arr2 = [["john", 55]]
[["steve", 33],["me", 22], ["john", 55]]
85
let array1 = [1,2,3]; let array2 = [4,5,6]; let array3 = // concat using ... spread syntax
[...array1,...array2];
86
let array1 = [1,2,3]; let array2 = [4,5,6]; let array3 = // concat using concat()
console.log(array1.concat(array2));
87
``` var blue = { color:blue; } ``` let colors = [{color:red}, {color:blue}]; var index = indexOf(blue) // returns
-1 | referencing the blue object
88
``` var blue = { color:blue; } ``` let colors = [{color:red}, blue]; var index = indexOf(blue) // returns
1 referencing from array
89
arr.indexOf(_________)
searchElement[, fromIndex]