Javascript Array Methods Flashcards
concat()
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
copyWithin()
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”]
entries()
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”]
every()
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
fill()
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”]
filter()
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]
find()
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
findIndex()
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
forEach()
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”
from()
Creates an array from an object
Array.from(object, mapFunction, thisValue)
console.log(Array.from(‘soccer’));
OUTPUT:
[“s”, “o”, “c”, “c”, “e”, “r”]
includes()
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
indexOf()
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
isArray()
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
join()
Joins all elements of an array into a string
keys()
Returns an Array Iteration object, containing the keys of the original array