Array Functions Flashcards

1
Q

Create an array from a string.

A

const someStr = ‘hello world’;

  • Array.from(someStr);
  • […someStr]
  • someStr.split(‘’);
  • Object.assign([], string);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Create an array with a function to map every element of the array.

A

Array.from([1, 2, 3], x => x + 1);

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

Create array with a number of empty slots

A

Array(3);

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

Initialize an array with values

A

Array.of(1, 2, 3);
const arr = [1, 2, 3];
Array(1, 2, 3);

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

Get 3rd to last index of an array

A

const arr = [1, 2, 3, 4, 5, 6];

console.log(arr.at(-3)];

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

combine 2 arrays. returning new array and without returning new array

A
const arr1 = [a, b, c];
const arr2 = [1, 2, 3];

arr1.concat(arr2);

[…ar1, …arr2];

[].concat(arr1, arr2);

arr1.push(…arr2);

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

Fill an array with values

A

fill(value)
fill(value, start)
fill(value, start, end)

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

conditional to see if every element of an array passes

A

[12, 54, 18, 130, 44].every(x => x >= 10); // true

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

find last element or index of an array given a testing function

A

array. findLast(…)

array. findLastIndex(…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
const arr2 = [0, 1, 2, [[[3, 4]]]]
how to turn that into [0, 1, 2, 3, 4]

how to turn that flat with a function to apply to every element?

A

with flat
console.log(arr2.flat(2));

with “flatMap”

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

determine whether the array contains a value.

A

[1, 2, 3].includes[1];

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

the last index of an element

A

[1, 2, 3, 2].lastIndexOf(2)

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

join all elements of an array to a string. also with a separator character

A

[1, 2, 3].join(‘-‘);

[1, 2, 3].toString(); // without separator

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

remove first element of array

A

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

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

add to front of the array

A

array1.unshift(4, 5); // also returns length of new array

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

find out if at least one element in the array satisfies a given function

A

[1, 2, 3].some(num => num < 4);

17
Q

return a portion of an array.

A

[1, 2, 3, 4, 5].slice(1, 3); // [2, 3]

18
Q

give splice parameters and what does it return

A

splice(start, deleteCount, itemToAdd)

returns the array of what was removed