Javascript Array Methods Flashcards

1
Q

concat()

A

Joins arrays and returns an array with the joined arrays

let arrayOne = [“apples”, “oranges”];
let arrayTwo = [“grapes”, “strawberries”];
Let answer = arrayOne.concat(arrayTwo);
console.log(answer)

OUTPUT:
apples, oranges, grapes, strawberries

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

copyWithin()

A

Copies array elements within the array, to and from specified positions

array.copyWithin(target, start, end)

let fruit = [“soccer”, “football”, “basketball”, “baseball”, “tennis”];
let arrOne = fruit.copyWithin(1,3);
console.log(arrOne);

OUTPUT:
[“soccer”, “baseball”, “tennis”, “baseball”, “tennis”]

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

entries()

A

Returns a key/value pair Array Iteration Object

array.entries()

let sports = [“soccer”, “football”, “basketball”, “baseball”];
let answer = sports.entries();
console.log(answer.next().value);
console.log(answer.next().value);

OUTPUT:
[0, “soccer”]
[1, “football”]

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

every()

A

Checks if every element in an array pass a test

let scores = [95, 50, 75, 100, 80, 45]
const passed = (current) => current > 50
console.log(scores.every(passed));

OUTPUT:
false

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

fill()

A

Fill the elements in an array with with a static value

array.fill(value, start, end)

let sports = [“soccer”, “football”, “basketball”, “baseball”];
let answer = sports.fill(“soccer”);
console.log(answer)

OUTPUT:
[“soccer”, “soccer”, “soccer”, “soccer”]
——————————————————-

let sports = [“soccer”, “football”, “basketball”, “baseball”];
let answer = sports.fill(“soccer”, 2, 4);
console.log(answer)

OUTPUT:
[“soccer”, “football”, “soccer”, “soccer”]

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

filter()

A

Creates a new array with every element in an array that pass a test

array.filter(function(currentValue, index, arr), thisValue)

let scores = [95, 97, 35, 47, 85]
let pass = scores.filter(checkScore);

function checkScore(scores) {
return scores > 50;
}

OUTPUT:
[95, 97, 85]

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

find()

A

Returns the value of the first element in an array that pass a test

array.find(function(currentValue, index, arr),thisValue)

let scores = [95, 77, 50, 85, 100, 97]
let found = scores.find(a => a > 90)
console.log(found)

OUTPUT:
95

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

findIndex()

A

Returns the index of the first element in an array that pass a test. If none pass the test then -1 is returned

let scores = [20, 55, 35, 75, 85]
let pass = scores.findIndex(a => a > 70)
console.log(pass)

OUTPUT:
3

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

forEach()

A

Calls a function for each array element

array.forEach(function(currentValue, index, arr), thisValue)

let arrOne = [“apple”, “orange”, “banana”]
arrOne.forEach(a => console.log(a));

OUTPUT:
“apple”
“orange”
“banana”

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

from()

A

Creates an array from an object

Array.from(object, mapFunction, thisValue)

console.log(Array.from(‘soccer’));

OUTPUT:
[“s”, “o”, “c”, “c”, “e”, “r”]

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

includes()

A

Check if an array contains the specified element

let numbers = [1, 2, 3, 7, 9];
let answer = numbers.includes(9);
console.log(answer);

OUTPUT:
true

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

indexOf()

A

Search the array for an element and returns its position

string.indexOf(searchvalue, start)

let sports = [“soccer”, “football”, “basketball”, “baseball”, “soccer”];

let answer = sports.indexOf(“soccer”);
console.log(answer);

OUTPUT:
0

————————————————

let sports = [“soccer”, “football”, “basketball”, “baseball”, “soccer”];

let answer = sports.indexOf(“soccer”, 1);
console.log(answer);

OUTPUT:
4

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

isArray()

A

Checks whether an object is an array

Array.isArray(value)

let numbers = [2, 5, 7, 9]
let answer = Array.isArray(numbers)
console.log(answer)

OUTPUT:
true

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

join()

A

Joins all elements of an array into a string

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

keys()

A

Returns an Array Iteration object, containing the keys of the original array

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

lastIndexOf()

A

Search the array for an element, starting at the end, and returns it’s position

17
Q

length

A

Sets or returns the number of elements in an array

18
Q

map()

A

Creates a new array with the result of calling a function for each array element

19
Q

pop()

A

Removes the last element of an array, and returns that element

20
Q

push()

A

Adds new elements to the end of an array, and returns the new length

21
Q

reduce()

A

Reduce the values of an array to a single value (going left to right)

22
Q

reduceRight()

A

Reduce the values of an array to a single value (going right-to-left)

23
Q

reverse()

A

Reverse the order of the elements in an array

24
Q

shift()

A

Removes the first element of an array, and returns that element

25
Q

slice()

A

Selects a part of an array, and returns the new array

const animals = [‘soccer’, ‘football’, ‘baseball’, ‘tennis’]

console.log(animals.slice(2));

Output:
[‘baseball’, ‘tennis’]

26
Q

some()

A

Checks if any of the elements in an array pass a test

27
Q

sort()

A

Sorts the element of an array

28
Q

splice()

A

Adds/removes elements from an array

29
Q

toString()

A

Converts an array to a string, and returns the result

30
Q

unshift()

A

Adds new elements to the beginning of an array, and returns the new length

31
Q

valueOf()

A

Returns the primitive value of an array