Array methods Flashcards

1
Q

Array.prototype.at()

A

Takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.
~~~
[1, 2, 3].at(1) // will return 2
~~~

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

Array.prototype.concat()

A

Merge two or more arrays. Does not change the existing arrays, but instead returns a new array.
~~~
let newArr = [1,2,3].concat([4,5,6]) // will return [1,2,3,4,5,6]
~~~

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

Array.prototype.copyWithin()

A

Shallow copies part of this array to another location in the same array and returns this array without modifying its length.
Parameters - target (where to start copying to), start (where to start copying from), end (optional - where to stop copying from, if omited, copy to end of array up to length of the array)
~~~
[1,2,3,4,5].copyWithin(1, 3) // will return [1, 4, 5, 4, 5]
~~~

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

Array.prototype.every()

A

Tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
~~~
[1,2,3,4,5].every(item => item < 10) // returns true
~~~

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

Array.prototype.fill()

A

Changes all elements within a range of indices in an array to a static value. It returns the modified array.
Parameters - value (what to fill array with), start (optional - where to start filling), end (optional - where to end filling, up to and not including end, if absent fill to end)
~~~
[1,2,3,4,5].fill(1, 5) // returns [1, 5, 5, 5, 5]
~~~

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

Array.prototype.filter()

A

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
~~~
[1, 2, 3, 4, 5].filter(item => item % 2 == 0) // returns [2, 4]
~~~

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

Array.prototype.find()

A

Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
~~~
[5, 8, 12, 25, 114].find(item => item > 10) // returns 12
~~~

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

Array.prototype.findIndex()

A

Returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
~~~
[5, 8, 12, 56, 145].findIndex(item => item > 13) // returns 3
~~~

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

Array.prototype.findLast()

A

Iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned.
e.g.
~~~
[5, 8, 50, 130, 12].findLast(item => item > 30) //returns 130
~~~

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

Array.prototype.findLastIndex()

A

Iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
~~~
[5, 8, 130, 48, 12].findLastIndex(item => item > 30) // returns 3
~~~

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

Array.prototype.flat()

A

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Parameters - depth (optional - the depth level of the flattening, defaults to 1)
~~~
[1,2,[3,4]].flat() // returns [1,2,3,4]
~~~

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

Array.prototype.flatMap()

A

Returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1 (arr.map(…args).flat()), but slightly more efficient than calling those two methods separately.
~~~
[1, 2, 1}.flatMap((num) => (num === 2 ? [2, 2] : 1)) // returns [1, 2, 2, 1]
~~~

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

Array.prototype.forEach()

A

Executes a provided function once for each array element. Return value is undefined.
~~~
[1, 2, 3].forEach(item => console.log(item)) // logs each item to the console

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

Array.prototype.includes()

A

Determines whether an array includes a certain value among its entries, returning true or false as appropriate.
~~~
[5, 10, 15].includes(10) // returns true
~~~

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

Array.prototype.indexOf()

A

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

 [5, 10, 15, 20].indexOf(15) // returns 2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Array.prototype.join()

A

Creates and returns a new string by concatenating all of the elements in this array, 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.
~~~
[‘c’, ‘a’, ‘t’].join(“”) // returns ‘cat’
~~~

17
Q

*

Array.prototype.keys()

A

Returns a new array iterator object that contains the keys for each index in the array.

18
Q

Array.prototype.lastIndexOf()

A

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.
Parameters - search element (item searched for), fromindex (optional - index to start searching from)
~~~
[1, 2, 3, 1].lastIndexOf(1) // returns 3
~~~

19
Q

Array.prototype.map()

A

Creates a new array populated with the results of calling a provided function on every element in the calling array.
~~~
[1, 2, 3].map(item => item * 2) // returns [2, 4, 6]
~~~

20
Q

Array.prototype.pop()

A

Removes the last element from an array and returns that element. This method changes the length of the array.
~~~
[1, 2, 3, 4].pop() // returns 4
~~~

21
Q

Array.prototype.push()

A

Adds the specified elements to the end of an array and returns the new length of the array.
~~~
[‘apple’, ‘bears’, ‘cows’].push(‘dust’) // returns 4
~~~

22
Q

Array.prototype.reduce()

A

Executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
~~~
[1, 2, 3, 4].reduce((acc, curr) => acc + curr) // returns 10
~~~

23
Q

Array.prototype.reverse()

A

Reverses an array in place and returns the reference to the same array, the first array element now becoming the last, and the last array element becoming the first. In other words, elements order in the array will be turned towards the direction opposite to that previously stated.
~~~
[1, 2, 3].reverse() // returns [3, 2, 1]

24
Q

Array.prototype.shift()

A

Removes the first element from an array and returns that removed element. This method changes the length of the array.
~~~
[1, 2, 3, 4].shift() // returns 1
~~~

25
Q

Array.prototype.slice()

A

Returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
Parameters - start (index to start slicing), end (optional, slice up to but not including end)
~~~
[1, 2, 3, 4, 5].slice(2) // returns [3,4,5]
~~~

26
Q

Array.prototype.some()

A

Tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.
~~~
[1, 2, 3, 4, 5].some(item => item % 2 === 0) // returns true
~~~

27
Q

Array.prototype.sort()

A

Sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
~~~
[‘cow’, ‘apple’, ‘bison’].sort() // returns [‘apple’, ‘bison’, ‘cow’]
[1, 56, 1000, 2].sort() // returns [1, 1000, 2, 56] -> this needs to be [1, 56, 1000, 2].sort((a, b) => a-b) to give [1, 2, 56, 1000]
~~~

28
Q

Array.prototype.splice()

A

Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Parameters - start (index to start splice), delete count (optional, how many items to remove), item1 , item2 etc, (what to insert at the splice)
~~~
[‘jan’, ‘march’, ‘april’].splice(1, 0, ‘feb’) // returns [‘jan’, ‘feb’, ‘march’, ‘april’]
[‘jan’, ‘feb’, march’, ‘may’].splice(3, 1, ‘april’) // returns [‘jan’, ‘feb’, ‘march’, ‘april’]
~~~

29
Q

Array.prototype.toReversed()

A

The copying counterpart of the reverse() method. It returns a new array with the elements in reversed order.

30
Q

Array.prototype.toSorted()

A

The copying version of the sort() method. It returns a new array with the elements sorted in ascending order.

31
Q

Array.prototype.toSpliced()

A

The copying version of the splice() method. It returns a new array with some elements removed and/or replaced at a given index.

32
Q

Array.prototype.toString()

A

Returns a string representing the specified array and its elements.

 [1, 2, 3].toString() // returns "1,2,3"
33
Q

Array.prototype.unshift()

A

Adds the specified elements to the beginning of an array and returns the new length of the array.
[1, 2, 3].unshift(5) // returns 4

34
Q

Array.prototype.values()

A

Returns a new array iterator object that iterates the value of each item in the array.

35
Q

Array.prototype.with()

A

The copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value.
Parameters - index (where to make the change), value (what to replace item at given index with)
~~~
[1, 2, 3, 4, 5].with(2, 6) // returns [1, 2, 6, 4, 5]
~~~