Array Methods Flashcards

1
Q

How would I obtain the length of an array?

A

array.length

const myArray = [1, 2, 3, 4]
console.log(myArray.length) // prints 4

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

How would I add one or more elements to the end of an array?

A

array.push()

const myArray = [1, 2, 3, 4]
console.log(myArray.push(5)) // prints [1, 2, 3, 4, 5]

Returns the new length of the array.

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

How would I remove and return the last element from an array?

A

array.pop()
~~~
const myArray = [1, 2, 3, 4]
const lastElement = myArray.pop()
~~~

console.log(myArray) // prints [1, 2, 3]
console.log(lastElement) // prints 4

Mutates the original array.

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

How would I remove and return the first element of an array?

A

array.shift()
~~~
const myArray = [1, 2, 3, 4];
const firstElement = myArray.shift();
~~~

console.log(myArray); // prints [2, 3, 4]
console.log(firstElement); // prints 1

Mutates the original array.

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

How would I add one or more elements to the beginning of an array?

A

array.unshift()
~~~
const myArray = [1, 2, 3, 4];
myArray.unshift(0);

console.log(myArray);
``` // prints [0, 1, 2, 3, 4]

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

How would I return a portion of an array as a new array?

A

array.slice(a, b)
Parameter a is starting index.
Parameter b is ending ending index, which is not included.

const myArray = [1, 2, 3, 4];
const newArray = myArray.slice(1,  3)

console.log(newArray) // prints [2, 3];
console.log(myArray) // prints [1, 2, 3, 4];

The original array is not mutated.

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

How would I remove and/or replace existing elements at any location in an array?

A

array.splice(a, b, c)
Parameter a is starting index (required)
Parameter b is number of elements to remove (required)
Paremeter c is one or more elements to be inserted (optional)

~~~
const myArray = [1, 2, 3, 4];
my array.splice(2, 1, “a”, “b”);

console.log(myArray)
``` // prints [1, 2, “a”, “b”, 4]

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

How would I combine two or more arrays?

A

Method 1:
array.concat()
~~~
const array1 = [1, 2]
const array2 = [3, 4]
const array3 = [5, 6]
const combinedArray = array1.concat(array2, array3)
~~~

Method 2:
Spread operator

const combinedArray = [...array1, ...array2, ...array3]

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

How would I check whether all elements in an array pass a certain condition?

A

array.every(function)
~~~
const numbers = [2, 4, 6, 8];
const areAllEven = numbers.every(num => num % 2 === 0);

console.log(areAllEven);
``` // true

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

How would I create a new array with all elements that pass a provided test?

A

array.filter(function)

~~~
function isBigEnough(value) {
return value >= 10;
}

const filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
``` // filtered is [12, 130, 44]

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

How would I return the value of the first element in an array that satisfies a provided test?

A

array.find(function)

~~~
const numbers = [2, 4, 6, 8, 10];
const foundNumber = numbers.find(function (num) {
return num > 5;
})

console.log(foundNumber)
``` // 6

Returns undefined if no element satisfies condition.

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

How would I return the index of the first element in an array that satisfies a provided test?

A

array.findIndex(function)

~~~
const numbers = [2, 4, 6, 8, 10];
const firstEvenIndex = numbers.findIndex(num => num % 2 === 0);

console.log(firstEvenIndex)
``` // 0

Returns -1 if no elements pass the test.

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

How would I create a new array with sub-array elements flatted by a specified depth?

A

array.flat()

~~~
const arr = [1, 2, [3, 4]];
const flattened = arr.flat();

console.log(flattened);
``` // [1, 2, 3, 4]

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

How would I loop through an array and perform a function on each element using the .forEach() method?

A

array.forEach(element, index, array)

element parameter is current value of element being processed
index parameter is index of current element being processed
array parameter is array that forEach() is being called on

Example : Looping through array and logging each element

const myArray = ['apple', 'banana', 'orange'];
myArray.forEach((element) => {
  console.log(element); //apple banana orange
});

Example 2: Modifying the elements of an array

~~~
const myArray = [1, 2, 3];

myArray.forEach((element, index, array) => {
array[index] = element * 2;
});

console.log(myArray);
``` //[2, 4, 6]

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

How would I check whether an array contains a certain value?

A

array.includes(value)
const arr = ["apple", "banana", "orange"];

console.log(arr.includes("banana")); // true
console.log(arr.includes("BANANA")); // false
console.log(arr.includes("grape")); // false

Returns boolean value.

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

How would I find the index of a certain element in an array?

A

array.indexOf(a, b)
Parameter a is the element sought
Parameter b is the starting index from which to search (optional)

const animals = [“cat”, “dog”, “fish”];
const index = animals.indexOf(“bird”);
console.log(index); // Output: -1

Returns -1 if the element is not found

17
Q

How would I concatenate all elements of an array into a single string?

A

array.join(separator)
The separator parameter is optional and will default to a comma if nothing else is provided. Otherwise, joins the elements of the array using the separator.

const fruits = [‘apple’, ‘banana’, ‘kiwi’];
const fruitsString = fruits.join(); // ‘apple,banana,kiwi’

18
Q

How would I locate the index of the last occurance of a specified value in an array?

A

array.lastIndexOf(a, b)
Parameter a is the value
Parameter b is index from which to start the search (optional)

Searches array from right to left.

19
Q

How would I reverse the order of the elements in an array?

A

array.reverse()

Mutates the original array

20
Q

How would I determine whether at least one element in an array meets a certain condition?

A

array.some(function(currentValue, index, array))

const numbers = [1, 5, 8, 12, 3];
const result = numbers.some(function(num, index) {
return num > index;
});

console.log(result); // true

Returns boolean

21
Q

How would I sort the elements of an array in place?

A

array.sort()

const numbers = [4, 2, 7, 1, 3];
numbers.sort();

console.log(numbers); // Output: [1, 2, 3, 4, 7]

Using .sort() with a compare function:

const numbers = [4, 2, 7, 1, 3];
numbers.sort((a, b) => a - b);
console.log(numbers); // Output: [1, 2, 3, 4, 7]

22
Q

How would I return a string representation of an array?

A

array.toString()
Returns elements of the array separated by commas, with no spaces beteween the elements.

const fruits = [‘apple’, ‘banana’, ‘cherry’];
const fruitsString = fruits.toString();

console.log(fruitsString); // “apple,banana,cherry”

23
Q

How would I create a new array from an interable object?

A

array.from()
console.log(Array.from(‘foo’)) // [‘f’, ‘o’, ‘o’]
console.log(Array.from([1, 2, 3], x => x + x)) // [2, 4, 6]

24
Q

How would I change all elements in an array to a static value?

A

array.fill()
let a = [1, 2, 3, 4]
let b = a.fill(0, 2, 4)
console.log(b) // [1, 2, 0, 0]

25
Q

How would I test whether a variable is an array?

A

array.isArray()

Returns boolean