Arrays Flashcards

1
Q

Arrays that only have one level, or that it does not have any other arrays nested within it.

A

One-dimensional array

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

An array that contains other arrays

A

Multi-dimensional array

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

The method that adds elements to the end of an array

A

push() method

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

The method that adds elements to the beginning

A

unshift() method

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

The method that removes an element from the end of an array

A

pop() method

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

The method that removes an element from the beginning of an array.

A

shift() method

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

The method that remove any number of consecutive elements from anywhere in an array.

A

splice()

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

map()

A
Creates a new array with the results of calling a provided function on every element in the calling array.
(It performs a function on every element of the array and results in a new array)
eg.)
var array1 = [1, 4, 9, 16];
// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

find()

A
The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
(It performs the given function to the array and spits back the first true element)
eg.)
var array1 = [5, 12, 8, 130, 44];
var found = array1.find(function(element) {
  return element > 10;
});
console.log(found);
// expected output: 12
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

findIndex()

A
The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
eg.)
var array1 = [5, 12, 8, 130, 44];
function isLargeNumber(element) {
  return element > 13;
}
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

filter()

A
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
eg.)
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

reduce()

A
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
eg.) const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

concat()

A
The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
eg.)
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.log(array1.concat(array2));
// expected output: Array ["a", "b", "c", "d", "e", "f"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

slice()

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). The original array will not be modified.
eg.) 
var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

splice()

A

The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
eg.)
var months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice(1, 0, ‘Feb’);
// inserts at index 1
console.log(months);
// expected output: Array [‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘June’]

months.splice(4, 1, ‘May’);
// replaces 1 element at index 4
console.log(months);
// expected output: Array [‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘May’]

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