Array Methods Flashcards

1
Q

Check whether a thing is an array

SIMPLE LANGUAGE:
Is the argument an Array?

A

Array.isArray(input)

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

Iterate over each array element. The return from a callback function is passed to the next iteration.

SIMPLE LANGUAGE:
Analyze all array elements, allow a variable to pass from one iteration to the next

A

myArray.reduce(callback function(acc, arg) => {
acc += arg}, accStart
)

ex.
> let arr = [2, 3, 5, 7]
> arr.reduce((accumulator, element) => accumulator + element, 0)

let apple = [1, 2, 3, 4, 5, 6]
console.log(apple.reduce((acc, value) =>
[(acc[0] + value), (acc[1] - value)], [0,0] ));

logs: [ 21, -21 ]

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

Analyze all of an array’s elements, returns the elements in a new array based on whether a function for each element is true.

SIMPLE LANGUAGE:
Create a new array from old array filtered based on some condition.

A

myArray.filter(element => if true return element as part of new array)

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

Return boolean if a string contains a specified substring
or
if an array element matches the entirety of the argument

SIMPLE LANGUAGE:
Does the array contain this entry?
Does the string contain this substring?

A

myStringOrArray.includes(‘value’)

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

SIMPLE LANGUAGE:

Cut (mutate) an array and return the cut section

A

myArray.splice(start index, number of indexes)

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

SIMPLE LANGUAGE:

Add argument to the end of an array and return the new array length

A

myArray.push(new element);

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

SIMPLE LANGUAGE:

Add argument to the beginning of an array and return the new array length

A

myArray.unshift(new element);

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

Return a combined array or string from the argument.
Returns string if method is used on a string.
Returns array if method is used on an array.

SIMPLE LANGUAGE:
Add string arguments to the end of a string
Add arrays, or other arguments to the end of an array.

A

myStringorArray.concat( , )

does not mutate.

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

Remove the last element from an array and return the element

A

myArray.pop();

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

Remove the first element from an array and return the element

A

myArray.shift();

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

Return a copied section from an array (or string)

What happens if the first argument equals the second?

What happens if there is no second argument?

A

myArray.slice(first index, last index exclusive)

If first index = last index, an empty array is returned

It returns an array/string from that index to the last

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

Reorganize an array’s elements in place (mutating). It also returns the organized array

No arg defaults to string comparison

A

myArray.sort( )

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

Reverse an array’s elements in place (mutating).

It also returns the reversed array

A

Array.prototype.reverse( )

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

Return the Array with nested arrays unnested.

can choose level of unesting

A

myArray.flat( number of levels to unnest)

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

Return combined array elements into a string with argument in between

A

myArray.join(‘ ‘)

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

What’s the difference between Array.from and .map() ?

A

Array.from() works with array-like objects like strings
Array.from() is meant to create arrays from array like objects and can take a callback function

.map() is for arrays
.map() is meant to map a new array off an existing one using a callback function. It has more functionality
.map() is 15 times faster

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

Return true if at least 1 array element returns true from a callback function

SIMPLE LANGUAGE:
Do some of the array element follow this pattern?

A

myArray.some( )

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

Return true if all array elements returns true from a callback function

SIMPLE LANGUAGE:
Does every array element follow this pattern?

A

myArray.every()

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

What’s the difference between .some() and .includes()?

A

.includes() is just a simple comparison of the argument and array values.
.some() lets you use a callback function to test whatever logic you want.

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

Returns the first element that returns true from a callback function.

SIMPLE LANGUAGE:
What’s the first element in the array that follows this pattern?

A

myArray.find( callback fuction)

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

Replace an array’s elements between certain indexes with a SINGLE entry
Returns the changed array

SIMPLE LANGUAGE
Replace each consecutive elements with a 1 argument.

A

myArray.fill(replacement, start ind, end exclusive index)
mutates
[1, 2, 3, 4, 5, 6, 7, 8].fill(“apple”, 4, 6)
Expected output:
[ 1, 2, 3, 4, ‘apple’, ‘apple’, 7, 8 ]

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

Return the Array elements based on a callback function and with first-level nested arrays unnested.

Does it map first or flat first?

SIMPLE LANGUAGE
Map an array based on callback and flat the result

A

myArray.flatMap( callback function)

It maps then flats.

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

What is returned from:
[1,2].concat([3,4]);

[1,2].concat([[3,4]]);

‘string’.concat(342432)

A

output=> [1,2,3,4]

output=> [1,2,[3,4]]

output=> “string342432”

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

What is returned from:

[1, 2, 3].pop().shift()

A

an error
Because it is evaluated from left to right
[1, 2, 3].pop() evaluates to 3
and 3.shift() give an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What are the similarities and difference between .substring() and .slice()? What happens when only 1 argument? If start index > end index, what happens? What do indexes start and finish? what do they work on? What happens when second argument larger than length? how do they repond to negative numbers or NaN?
Similarities - Both go from start index (inc) to end index (exc) - If end index is excluded they go to the end - If end index > length, end index = length; Differences - slice works with strings and arrays, substring only works on strings. - If start index > end index, .slice() will return empty string/ array. .substring() will swap the indexes. - both act different with negative arguments and NaN https: //stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring
26
What is logged to console from this: let apple = [1, 2, 3, 4, 5, 6] console.log(apple.reduce((acc, value) => [(acc[0] + value), (acc[1] - value)], [0,0] )); Why?
logs: [ 21, -21 ] The accumulator is set to: [0,0] The callback function returns 2 elements in an array (acc[0] + value) and (acc[1] - value) The first accumulator element plus the value The second accumulator element minus the value
27
``` What is the difference between each of these (what do they do and return?): Array.prototype.includes() Array.prototype.some( ) Array.prototype.every() Array.prototype.filter() Array.prototype.find() Array.prototype.indexOf() ```
.includes() - returns boolean based on whether the argument is within the array .some( ) - iterates over each element and returns boolean if at least 1 iteration returns true based on a callback function .every() - iterates over each element and returns boolean if all iterations return true based on a callback function .filter() - iterates over each element and returns an array based on whether the elements evaluate to true based on a callback function .find() - returns the value of the first element that returns true from a callback function .indexOf() returns the first index of an element that matches the argument
28
What's the difference between .map() and .forEach() ? | 2 differences
.forEach does a function for each element in an array. .map() returns an array based on a callback function return. 1.  .forEach() returns undefined (and can only be used at the end of a chain usefully) .map() returns the new mapped array (and is therefore chainable from) 2. .map() does not mutate and returns an array .forEach() just allows a callback function iterating over array elements
29
What does Array.prototype.includes() do? and/or return?
Determines whether an array includes a certain value among its entries, returning true or false as appropriate. includes(searchElement) includes(searchElement, fromIndex) (There is also a String.prototype.includes())
30
What does Array.isArray() do? and/or return?
Returns boolean based on whether the passed value is an Array. Array.isArray(value)
31
What does Array.prototype.reduce() do? and/or return?
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. ``` // Arrow function reduce((previousValue, currentValue) => { /* ... */ } ) reduce((previousValue, currentValue, currentIndex) => { /* ... */ } ) reduce((previousValue, currentValue, currentIndex, array) => { /* ... */ } ) reduce((previousValue, currentValue, currentIndex, array) => { /* ... */ }, initialValue) ``` // Callback function reduce(callbackFn) reduce(callbackFn, initialValue) ``` // Inline callback function reduce(function(previousValue, currentValue) { /* ... */ }) reduce(function(previousValue, currentValue, currentIndex) { /* ... */ }) reduce(function(previousValue, currentValue, currentIndex, array) { /* ... */ }) reduce(function(previousValue, currentValue, currentIndex, array) { /* ... */ }, initialValue) ```
32
What does Array.prototype.filter() do? and/or return?
Creates a new array with all elements that pass the test implemented by the provided function. ``` // Arrow function filter((element) => { /* ... */ } ) filter((element, index) => { /* ... */ } ) filter((element, index, array) => { /* ... */ } ) ``` // Callback function filter(callbackFn) filter(callbackFn, thisArg) ``` // Inline callback function filter(function(element) { /* ... */ }) filter(function(element, index) { /* ... */ }) filter(function(element, index, array){ /* ... */ }) filter(function(element, index, array) { /* ... */ }, thisArg) ```
33
What does Array.prototype.splice() do? and/or return?
changes the contents of an array by removing or replacing existing elements and/or adding new elements in place Returns the cut section splice(start) splice(start, deleteCount) splice(start, deleteCount, item1) splice(start, deleteCount, item1, item2, itemN)
34
What does Array.prototype.push() do? and/or return?
adds one or more elements to the end of an array and returns the new length of the array. push(element0) push(element0, element1) push(element0, element1, /* ... ,*/ elementN)
35
What does Array.prototype.unshift() do? and/or return?
adds one or more elements to the beginning of an array and returns the new length of the array. unshift(element0) unshift(element0, element1) unshift(element0, element1, /* ... ,*/ elementN)
36
What does Array.prototype.concat() do? and/or return?
The concat() method combines two or more arrays AND/OR adds the arguments to the target array. This method does not change the existing arrays, but instead returns a new array. concat() concat(value0) concat(value0, value1) concat(value0, value1, ... , valueN) (There is also a String.prototype.concat())
37
What does Array.prototype.pop() do? and/or return?
removes the last element from an array and returns that element. This method changes the length of the array. pop()
38
What does Array.prototype.shift() do? and/or return?
removes the first element from an array and returns that removed element. This method changes the length of the array. shift()
39
What does Array.prototype.slice() do? and/or return?
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. slice() slice(start) slice(start, end)
40
What does Array.prototype.sort() do? and/or return? How does it's algorithm work?
sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values. Mutates ``` // Functionless sort() ``` ``` // Arrow function sort((firstEl, secondEl) => { /* ... */ } ) ``` ``` // Compare function sort(compareFn) ``` ``` // Inline compare function sort(function compareFn(firstEl, secondEl) { /* ... */ }) ``` If [1, -2, 15, 2, 0, 8].sort(function(a, b) { }); It will put a number in a It will put a number in b If the return of the function is positive, it will put b after a.
41
What does Array.prototype.reverse() do? and/or return?
reverses an array in place. The first array element becomes the last, and the last array element becomes the first. Mutates Returns the reversed array reverse()
42
What does Array.prototype.flat() do? and/or return?
creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. flat() flat(depth)
43
What does Array.prototype.join() do? and/or return?
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. If the array has only one item, then that item will be returned without using the separator. join() join(separator)
44
What does Array.from() do? and/or return?
static method creates a new, shallow-copied Array instance from an array-like or iterable object. ``` // Arrow function Array.from(arrayLike, (element) => { /* ... */ } ) Array.from(arrayLike, (element, index) => { /* ... */ } ) ``` ``` // Mapping function Array.from(arrayLike, mapFn) Array.from(arrayLike, mapFn, thisArg) ``` ``` // Inline mapping function Array.from(arrayLike, function mapFn(element) { /* ... */ }) Array.from(arrayLike, function mapFn(element, index) { /* ... */ }) Array.from(arrayLike, function mapFn(element) { /* ... */ }, thisArg) Array.from(arrayLike, function mapFn(element, index) { /* ... */ }, thisArg) ```
45
What does Array.prototype.map() do? and/or return?
creates a new array populated with the results of calling a provided function on every element in the calling array. ``` // Arrow function map((element) => { /* ... */ }) map((element, index) => { /* ... */ }) map((element, index, array) => { /* ... */ }) ``` ``` // Callback function map(callbackFn) map(callbackFn, thisArg) ``` ``` // Inline callback function map(function(element) { /* ... */ }) map(function(element, index) { /* ... */ }) map(function(element, index, array){ /* ... */ }) map(function(element, index, array) { /* ... */ }, thisArg) ```
46
What does Array.prototype.some() do? and/or return?
"Are there some values in the array for which the given callback returns a truthy value? 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. ``` // Arrow function some((element) => { /* ... */ } ) some((element, index) => { /* ... */ } ) some((element, index, array) => { /* ... */ } ) ``` // Callback function some(callbackFn) some(callbackFn, thisArg) ``` // Inline callback function some(function(element) { /* ... */ }) some(function(element, index) { /* ... */ }) some(function(element, index, array){ /* ... */ }) some(function(element, index, array) { /* ... */ }, thisArg) ```
47
What does Array.prototype.every() do? and/or return?
The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. ``` // Arrow function every((element) => { /* ... */ } ) every((element, index) => { /* ... */ } ) every((element, index, array) => { /* ... */ } ) ``` // Callback function every(callbackFn) every(callbackFn, thisArg) ``` // Inline callback function every(function(element) { /* ... */ }) every(function(element, index) { /* ... */ }) every(function(element, index, array){ /* ... */ }) every(function(element, index, array) { /* ... */ }, thisArg) ```
48
What does Array.prototype.find() do? and/or return?
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned. ``` // Arrow function find((element) => { /* ... */ } ) find((element, index) => { /* ... */ } ) find((element, index, array) => { /* ... */ } ) ``` // Callback function find(callbackFn) find(callbackFn, thisArg) ``` // Inline callback function find(function(element) { /* ... */ }) find(function(element, index) { /* ... */ }) find(function(element, index, array){ /* ... */ }) find(function(element, index, array) { /* ... */ }, thisArg) ```
49
What does Array.prototype.fill() do? and/or return?
The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length). It returns the modified array. Mutates fill(value) fill(value, start) fill(value, start, end)
50
What does Array.prototype.flatMap() do? and/or return?
The flatMap() method 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, but slightly more efficient than calling those two methods separately. ``` // Arrow function flatMap((currentValue) => { /* ... */ } ) flatMap((currentValue, index) => { /* ... */ } ) flatMap((currentValue, index, array) => { /* ... */ } ) ``` // Callback function flatMap(callbackFn) flatMap(callbackFn, thisArg) ``` // Inline callback function flatMap(function(currentValue) { /* ... */ }) flatMap(function(currentValue, index) { /* ... */ }) flatMap(function(currentValue, index, array){ /* ... */ }) flatMap(function(currentValue, index, array) { /* ... */ }, thisArg) ```
51
What does Array.prototype.indexOf() do? and/or return?
returns the first index at which a given element can be found in the array, or -1 if it is not present. indexOf(searchElement, fromIndex) (There is also an indexof for strings)
52
``` Which of these take a callback function: Array.prototype.includes() Array.prototype.some( ) Array.prototype.every() Array.prototype.filter() Array.prototype.find() Array.prototype.indexOf() ```
.some() .every() .filter() .find()
53
Return the first index in an array that matches the given argument Can also start from a given index
Array.prototype.indexOf()
54
What's the difference between .map() and .filter() and Array.from()
.filter() creates a new array based on whether the elements return boolean on a callback function .map creates a new array based on the return of a callback function Array.from() is for array like object. It is slower than .map. but otherwise functions similarly
55
Return the elements of an array as a string separated by commas 2 ways
Array.prototype.toString() | String(arr)
56
What does: Array.prototype.toString() do and/or return?
Return the elements of an array as a string separated by commas
57
t or f. Why? alert( 0 == [0] ); alert( '0' == [0] ); alert( [0] == [0] ); alert( 0 == [] ); alert( '' == [] ); alert('0' == [] );
false- non empty arrays compared to primitives will convert to strings, '0' true- false- these arrays are unique objects true- empty arrays will convert to 0 when compared to numbers true- empty arrays will convert to '' when compared to strings false- '0' == ''
58
Insert elements into the middle of an array
arr.splice( 2, 0, 'insert this', 'and this') | It wil insert it AT position 2. It will push the old position 2 to position 3
59
What happens when you use negative indexes with array methods? How about as an array index? what is returned from each of these? arr[-1] arr[-1] = 1; arr[-1]
It starts from the end of the array. undefined, looking for a string '-1' as a key likely makes the -1 a key as a regular object property returns 1 as the object value it will be listed in the array as: '-1': 1
60
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.
Array.prototype.findIndex()
61
What does: Array.prototype.findIndex() do and/or return?
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.
62
How is the array altered? | [9, 21, 1].sort()
``` [1, 21, 9] The elements are sorted as strings. How do you make it not do this? (scroll down) . . . . . . ``` ``` . . . . . . . . ``` . . . To make it not do this: ``` function compare(a, b) { if (a > b) return 1; // if the first value is greater than the second if (a == b) return 0; // if values are equal if (a < b) return -1; // if the first value is less than the second } [9, 21, 1].sort(compare() ) ``` ``` // Functionless sort() ``` ``` // Arrow function sort((firstEl, secondEl) => { /* ... */ } ) ``` ``` // Compare function sort(compareFn) ``` ``` // Inline compare function sort(function compareFn(firstEl, secondEl) { /* ... */ }) ```
63
What does Array.prototype.reduceRight() do? and/or return?
same as reduce but starts on the end of the array
64
Do a reduce method but start from the end
Array.prototype.reduceRight()
65
What is "thisArg"?
A parameter in many methods that sets the value of this. thisArg refers to context which callback should be called, basically it is what this refers to inside callback. For example: var myObject = { name: 'myObject' }; [1,2].forEach(function(item) { console.log(item); // 1, 2 console.log(this === myObject); // true }, myObject)