Array Methods Reference
No description
Array.prototype.forEach()
Array.prototype.forEach()
Called on the array.
Takes a function as an argument.
Simpler way to iterate over an array
Often uses arrow functions
() => {...}
Second Argument
index position of the element in the array is optional
forEach()
Example
forEach()
function()
[1, 2, 3].forEach((number, idx) => {
console.log(`${idx}: ${number}`);
});
Strings
use
split()
to process every character in a string with forEach()
'abcd'.split('').forEach(char => {
console.log(char);
});
Objects
to use with
forEach()
convert to array with Object.keys()
, Object.values()
and Object.entries()
Values
let produceValues = Object.values(produce);
produceValues.forEach(value => {
console.log(value);
});
Keys
let produceKeys = Object.keys(produce);
produceKeys.forEach(key => {
console.log(key);
});
Entries / Key-value pairs
let produceKeys = Object.keys(produce);
produceKeys.forEach(key => {
console.log(key);
});
Array Destructuring Assignment
Assign elements of the array to multiple variables
wrap the variable names in brackets
let [key, value] = keyValue
Array.prototype.filter()
Array.prototype.filter()
- for selecting or filter elements
examines the return value of the callback on each Iteration.filter()
- determines the truthiness of the return value
- selects element if the return value of the callback is truthy
- does not select the element if the return value is falsy
returns new array containing selected elementsfilter()
- the callback must return an explicit value or else it will return an empty array
- use
for filtering objectsforEach()
filter()
Example
filter()
[1, 2, 3].filter(num => num % 2 === 1);
// [ 1, 3 ]
oddNumbers; // => [1, 3]
Filtering objects with forEach()
forEach()
let produce = {
apple: 'Fruit',
carrot: 'Vegetable',
pear: 'Fruit',
broccoli: 'Vegetable'
};
let produceKeyValues = Object.entries(produce);
let onlyVegetables = {};
produceKeyValues.forEach(keyValue => {
let [ key, value ] = keyValue;
if (value === 'Vegetable') {
onlyVegetables[key] = value;
}
});
onlyVegetables;
// => {carrot: 'Vegetable', broccoli: 'Vegetable'}
Array.prototype.map()
Array.prototype.map()
- considers the return value of the callback
- performs transformation using the return value of the callback
- places transformed value into a new array
- avoid
by always giving callback an explicit return value[undefined,...]
map()
Example
map()
[1, 2, 3].map(num => num * 2);
filter()
&& map()
with Strings
filter()
map()
array firstsplit()
- perform selection and/or transformation
- then
join()
- strings can be considered arrays theoretically
let str = "What's up, Doc?";
str.split('') .filter(char => 'aeiou'.includes(
char.toLowerCase())
).join('');
// => 'auo'
let str = "What's up, Doc?";
str.split('').filter(
char => 'aeiou'.includes(char.toLowerCase())
).join('');
// => 'auo'
Array.prototype.some()
Array.prototype.some()
- tests whether at least one element in the array passes the test
- the test is provided by the callback function from the first argument
does not modify the original arraysome()
returns booleansome()
if the callback returns a truthy value for at least one elementtrue
- Otherwise
false
- To use with objects use
&&Object.keys
Object.values
some()
Example
some()
[1, 2, 3].some(num => num > 2); //true
[1, 2, 3].some(num => num > 3); //false
some()
for objects
some()
let animals = {
a: 'ant',
b: 'bear',
c: 'cat'
};
Object.values(animals).some(
animalName => animalName.length > 4
); // => false
Object.values(animals).some(
animalName => animalName.length > 3
); // => true
Array.prototype.every()
Array.prototype.every()
- returns true if the callback's return value in every iteration is truthy
every()
example
every()
[1, 2, 3].every(num => num > 2); //false
[3, 4, 5].every(num => num > 2); // true;
every()
for objects
every()
let animals = {
a: 'ant',
b: 'bear',
c: 'cat'
};
Object.values(animals).every(
animalName => animalName.length > 2
); // => true
Array.prototype.find()
Array.prototype.find()
- takes a callback function as an argument
- returns the first element which the callback returns a truthy value
- returns
if no elements return trueundefined
[2, 1, 4, 3, 5].find(num => num > 2) // 4
Array.prototype.findIndex()
Array.prototype.findIndex()
- similar to
find()
- returns index of truthy elements
- returns
if no matches are found-1
[2, 1, 4, 3, 5].findIndex(num => num < 1)
//-1
Array.prototype.reverse()
Array.prototype.reverse()
- reverses the elements of the array
- mutates the original array
- to prevent mutation: create shallow copy with
firstslice()
[1, 2, 5, 7, 3].reverse()
// [ 3, 7, 5, 2, 1 ]
Array.prototype.includes()
Array.prototype.includes()
- searches for
strictly equal to the argumentelement
- doesn't work for finding
objectsValue
- can use it to determine the existence of specific keys in objects
another method to check if a key existsObject.prototype.hasOWnProperty()
[2, 1, 3].includes(1); // true
[2, 1, 3].includes(5); // false
Array.prototype.fill()
Array.prototype.fill()
- changes all elements to a static value
- from a start index
- default:
0
- default:
- to an end index
- default:
Array.length
- default:
- returns modified array
- is a mutator method
- If start is negative, it is treated as
.Array.length + start
- If
is negative, it is treated asend
.Array.length + end
- If the first parameter is an object, each slot in the array will reference that object
Syntax
const array = []
array.fill(value)
array.fill(value, start)
array.fill(value, start, end)
Callbacks
The callback is a function/method as an argument for another function
code within the callback is used for each iteration
for each iteration,
forEach
sends the value of the current element to the callback