Array Methods Flashcards

1
Q

arr.push

A

Adds items to the end
arr.push(…items)

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

arr.pop()

A

Take away an item from the end

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

arr.shift()

A

takes away item from the beginning

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

arr.unshift()

A

adds item to the beginning
arr.unshift(…items)

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

splice()

A

swiss army knives of arrays- can insert, remove, and replace elements
arr.splice(start, delete, replace/insert)
returns array of removed elements
ex: let arr = [“I”, “study”, “JavaScript”, “right”, “now”];

// remove 2 first elements
let removed = arr.splice(0, 2);

alert( removed ); // “I”, “study” <– array of removed elements

let arr = [“I”, “study”, “JavaScript”];

// from index 2
// delete 0
// then insert “complex” and “language”
arr.splice(2, 0, “complex”, “language”);

alert( arr ); // “I”, “study”, “complex”, “language”, “JavaScript”

Negative indexes: specify position from end of array

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

arr.slice()

A

returns new array copying all items passed as parameters
arr.slice(start, end)
arr.slice() by itself creates a copy– used to make a copy and make changes

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

arr.concat()

A

creates a new array that includes values from other arrays and additional items
arr.concat(arg1, arg2…)
takes arrays and values as arguments
can’t pass objects unless use: Symbol.isConcatSpreadable property -> treated like an array

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

Symbol.isConcatSpreadable

A

Used so objects can be concatenated

let arr = [1, 2];

let arrayLike = {
0: “something”,
1: “else”,
[Symbol.isConcatSpreadable]: true,
length: 2
};

alert( arr.concat(arrayLike) ); // 1,2,something,else

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