Array Methods Flashcards

1
Q

What parameters can be passed to the “map” method’s callback function?

A

item, index, array

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

What is the format of the “map” method and what does it do?

Use multiplyByTwo as an example.

A

The “map” method creates a new array populated with the results of calling a provided function on every element in the calling array.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const oldArr = [1, 2, 3];
  function multiplyByTwo (item, index, array) {
         return item * 2;
  }

newArr = oldArr.map(multiplyByTwo);

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

List 4 array methods that return a new array

A

map, filter, reduce, slice

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

What is the format of the “filter” method and what does it do?

Use idOddNumber as an example.

A

The “filter” method creates a new array with all elements that pass the test implemented by the provided function.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const oldArr = [1, 2, 3];
  function isOddNumber(item, index, array) {
       return item % 2 !== 0;
  };

const newArr = oldArr.filter(isOddNumber);

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

What is the format of the “find” method and what does it do?

Use idEqualToTen as an example.

A

The “find” method returns the value of the first element in the provided array that satisfies the provided testing function.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const myArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
  function isEqualToTen(item, index, array) {
       return item === 10;
  };

const ten = oldArr.find(isEqualToTen);

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

What parameters can be passed to the “filter” method’s callback function?

A

item, index, array

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

What parameters can be passed to the “find” method’s callback function?

A

item, index, array

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

What parameters can be passed to the “forEach” method’s callback function?

A

item, index, array

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

What is the format of the “forEach” method and what does it do?

Use idEqualToTen as an example.

A

The “forEach” method executes a provided function once for each array element

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const oldArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
const newArr = [];
function isEqualToTen(item, index, array) {
       if(item === 10) {
         newArr.push(item);
    }
 };

oldArr.forEach(isEqualToTen);

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

What is the difference between “map” and “forEach”?

A

map returns a new array and forEach does not

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

What parameters can be passed to the “every” method’s callback function?

A

item, index, array

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

What is the format of the “every” method and what does it do?

Use isGreaterThanTen as an example.

A

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

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const myArr = [12, 13, 14, 27, 80];
function isGreaterThanTen(item, index, array) {
     if (item > 10) {
       return true;
    }
}

myArr.every(isGreaterThanTen);

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

What parameters can be passed to the “some” method’s callback function?

A

item, index, array

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

What is the format of the “some” method and what does it do?

Use isGreaterThanTen as an example.

A

The “some” method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const myArr = [1, 3, 12, 13, 14, 27, 80];
function isGreaterThanTen(item, index, array) {
     if (item > 10) {
       return true;
    }
}

myArr.some(isGreaterThanTen);

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

What is the format of the “reverse” method and what does it do?

A

The “reverse” method reverses and array

_________________________
const myArr = [1, 3, 12, 13, 14, 27, 80];
myArr.reverse();

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

What is the format of the “includes” method and what does it do?

A

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

_______________________
const myArr = [1, 3, 12, 13, 14, 27, 80];
myArr.includes(2);

17
Q

What parameters can be passed to the “includes” method’s callback function?

A

valueToFind, fromIndex

18
Q

List at least 8 array methods that alter an existing array

A

forEach, reverse, sort, splice, pop, push, shift, unshift

19
Q

What is the format of the “shift” method and what does it do?

A

The “shift” method removes the first element from an array and returns that removed element. This method changes the length of the array.

_______________________

20
Q

What is the format of the “unshift” method and what does it do?

A

The “unshift” method adds one or more elements to the beginning of an array and returns the new length of the array.

_______________________

21
Q

What is the difference between “shift” and “unshift”?

A

Shift removes an item from the array. Unshift adds an item to the array.

22
Q

What is the format of the “splice” method and what does it do?

A

The “splice” method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.

\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
const oldArr = ["maia", "matteo", "emilia", "lucia"];
const newArr = oldArr.splice("frankie",1,3);

//expect newArr = [“matteo”, “emilia”]

23
Q

What is the format of the “slice” method and what does it do?

A

The “slice” method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.

______________

const oldArr = ["maia", "matteo", "emilia", "lucia"];
const newArr = oldArr.slice(1,3);

//expect newArr = [“matteo”, “emilia”]

24
Q

What is the different between “map” and “filter”?

A

Map will always return an array of the same length whereas filter will return an array that contains a subset of the original array.

25
Q

What arguments can be passed to the “reduce” method’s callback function?

A

accumulator, item, index, array