Array Methods Flashcards

1
Q

let arr = [“I”, “go”, “home”];
delete arr[1];
alert( arr[1] ); // returns?

A

undefined

That’s natural, because delete obj.key removes a value by the key. It’s all it does. Fine for objects. But for arrays we usually want the rest of elements to shift and occupy the freed place. We expect to have a shorter array now.

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

let arr = [“I”, “go”, “home”];
delete arr[1];
alert( arr.length ); // returns?

A

3

now arr = [“I”, , “home”];

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

let arr = [“I”, “study”, “JavaScript”];
arr.splice(1, 1);
alert( arr ); // returns?

A

[“I”, “JavaScript”]

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

let arr = [“I”, “study”, “JavaScript”, “right”, “now”];
arr.splice(0, 3, “Let’s”, “dance”);

alert( arr ) // returns?

A

[“Let’s”, “dance”, “right”, “now”]

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

let arr = [“I”, “study”, “JavaScript”, “right”, “now”];

let removed = arr.splice(0, 2);

alert( removed ); // returns?

A

“I”, “study” <– array of removed elements

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

let arr = [“I”, “study”, “JavaScript”];

arr.splice(2, 0, “complex”, “language”);

alert( arr ); // returns?

A

“I”, “study”, “complex”, “language”, “JavaScript”

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

The method ________creates a new array that includes values from other arrays and additional items.

A

arr.concat

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

The _______ method allows to run a function for every element of the array.

A

arr.forEach

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

_______– looks for item starting from index from, and returns the index where it was found, otherwise -1.

A

arr.indexOf(item, from)

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

________item, from) – looks for item starting from index from, returns true if found.

A

arr.includes()

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

let arr = [1, 0, false];

alert( arr.indexOf(0) ); //
alert( arr.indexOf(null) ); //

A

1

-1

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

The method _______ is the same as indexOf, but looks for from right to left.

A

arr.lastIndexOf

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

Imagine we have an array of objects. How do we find an object with the specific condition?

A

arr.find(fn)

let result = arr.find(function(item, index, array) {
// if true is returned, item is returned and iteration is stopped
// for falsy scenario returns undefined
});

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

et users = [
{id: 1, name: “John”},
{id: 2, name: “Pete”},
{id: 3, name: “Mary”}
];

let user = users.find( );
write code

alert(user.name); // John

A

item => item.id == 1

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

const arr = [NaN];
alert( arr.indexOf(NaN) ); //
alert( arr.includes(NaN) );//

A

-1

true (correct)

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

The_____method creates a new array populated with the results of calling a provided function on every element in the calling array.

A

map()

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

let lengths = [“Bilbo”, “Gandalf”, “Nazgul”].map(item => item.length);
alert(lengths); //

A

5,7,6

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

let lengths = [“Bilbo”, “Gandalf”, “Nazgul”]

using map return results

alert(lengths); // 5,7,6

A

.map(item => item.length);

19
Q

The _____method sorts the elements of an array in place and returns the reference to the same array, now sorted.

A

sort()

20
Q

let arr = [ 1, 2, 15 ];

arr.sort();

alert( arr ); //

A

1, 15, 2

21
Q

in arrays all elements are converted to ______ for comparisons.

A

strings

22
Q

let arr = [1, 2];
alert( arr.concat([3, 4], 5, 6) ); //

A

1,2,3,4,5,6

23
Q

let arr = [1, 2];

let arrayLike = {
0: “something”,
length: 1
};

alert( arr.concat(arrayLike) ); //

A

1,2,[object Object]

24
Q

The _____method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function

A

filter()

25
Q

let arr = [ 1, 2, 15 ];

arr.sort
// write the sort function

alert(arr); // 1, 2, 15

A

(function(a, b) { return a - b; });

26
Q

arr.sort(function(a, b) { return a - b; });

WRITE AS AN ARROW

A

arr.sort( (a,b) => a - b );

27
Q

in arrays For many alphabets, it’s better to use _______ method to correctly sort letters

A

str.localeCompare

28
Q

The method ________reverses the order of elements in arr.

A

arr.reverse

29
Q

The _______method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array

A

split()

30
Q

let str = “test”;

alert( str.split(‘’) ); //

A

t,e,s,t

31
Q

The ______ method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

A

split()

32
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.

A

join()

33
Q

let arr = [‘Bilbo’, ‘Gandalf’, ‘Nazgul’];

let str =
// glue the array into a string using ;

alert( str ); // Bilbo;Gandalf;Nazgul

A

arr.join(‘;’);

34
Q

let arr = [‘Bilbo’, ‘Gandalf’, ‘Nazgul’];

let str = arr.join(‘;’); // glue the array into a string using ;

alert( str ); //

A

Bilbo;Gandalf;Nazgul

35
Q

When we need to iterate and return the data for each element – we can use

A

map.

36
Q

When we need to iterate over an array – we can use name all 3

A

for
for…of
for Each

37
Q

let arr = [1, 2, 3, 4, 5];

let result = arr.reduce((sum, current) => sum + current, 0);

alert(result); // returns and why

A

15

38
Q

let arr = [1, 2, 3, 4, 5];

// removed initial value from reduce (no 0)
let result = arr.reduce((sum, current) => sum - current);

alert( result ); //

A

-13

39
Q

let arr = [1, 2, 3, 4, 5];

let result = arr.reduce((sum, current) => sum + current, 4);

alert(result); //

A

19

40
Q

alert(typeof {}); //

A

object

41
Q

alert(typeof []); //

A

object

42
Q

alert(Array.isArray({})); //true or false

A

false

43
Q

alert(Array.isArray([])); // true or false

A

true