Arrays - methods Flashcards

1
Q

.concat()

A

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let newArray = arr1.concat(arr2);
console.log(newArray); // outputs [1, 2, 3, 4, 5, 6]

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

.slice()

A

let arr = [1, 2, 3, 4, 5];
let newArray = arr.slice(1, 3);
console.log(newArray); // outputs [2, 3]

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

.splice()

A

let arr = [1, 2, 3, 4, 5];
arr.splice(1, 2, 6, 7);
console.log(arr); // outputs [1, 6, 7, 4, 5]

The .splice() method modifies the original array by adding, removing, or replacing elements. It takes three arguments:

start: the index at which to start changing the array.
deleteCount: the number of elements to be removed.
element1, element2, …: the elements to be added to the array.
It returns an array containing the removed elements (if any).

Example:

javascript
Copy code
let arr = [1, 2, 3, 4, 5];
let removed = arr.splice(2, 2, 6, 7);
console.log(arr); // outputs [1, 2, 6, 7, 5]
console.log(removed); // outputs [3, 4]
In this example, the .splice() method removes two elements (index 2 and 3) from the arr array and replaces them with two new elements (6 and 7). The method returns the removed elements in the removed array.

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

.sort()

A

let arr = [3, 1, 5, 2, 4];
arr.sort();
console.log(arr); // outputs [1, 2, 3, 4, 5]

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

.reverse()

A

let arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr); // outputs [5, 4, 3, 2, 1]

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

.map()

A

let arr = [1, 2, 3, 4, 5];
let newArray = arr.map(x => x * 2);
console.log(newArray); // outputs [2, 4, 6, 8, 10]

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

.filter()

A

let arr = [1, 2, 3, 4, 5];
let newArray = arr.filter(x => x % 2 === 0);
console.log(newArray); // outputs [2, 4]

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

.reduce()

A

let arr = [1, 2, 3, 4, 5];
let sum = arr.reduce((acc, cur) => acc + cur, 0);
console.log(sum); // outputs 15

This method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
Example:

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

.reduce()

A

let arr = [1, 2, 3, 4, 5];
let sum = arr.reduce((acc, cur) => acc + cur, 0);
console.log(sum); // outputs 15

This method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.
Example:

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

.forEach()

A

let arr = [1, 2, 3, 4, 5];
arr.forEach(element => console.log(element));
// outputs:
// 1
// 2
// 3
// 4
// 5

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

.Shift()

A

let arr = [1, 2, 3, 4, 5];
let first = arr.shift();
console.log(arr);
// outputs: [2, 3, 4, 5]
console.log(first);
// outputs: 1

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

.unshift()

A

let arr = [1, 2, 3, 4, 5];
let len = arr.unshift(0);
console.log(arr);
// outputs: [0, 1, 2, 3, 4, 5]
console.log(len);
// outputs: 6

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

.pop()

A

let arr = [1, 2, 3, 4, 5];
let last = arr.pop();
console.log(arr);
// outputs: [1, 2, 3, 4]
console.log(last);
// outputs: 5

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

.push()

A

let arr = [1, 2, 3, 4, 5];
let len = arr.push(6);
console.log(arr);
// outputs: [1, 2, 3, 4, 5, 6]
console.log(len);
// outputs: 6

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