Important Array and Object Iteration Methods
To locate elements in an array, we can use 2 different JavaScript methods:
Array.prototype.indexOf()
It is called on an array and takes 2 arguments.
The value you are looking for and an optional start position
compares each element using a strict equality operator (===)
returns the first INDEX of the first matching element
returns -1 if nothing is found in the array that matches
Array.prototype.find()
takes a callback function as an argument
it will iterate through the whole array and return the first element that satisfies the condition in the function.
undefined is returned if nothing satisfies the callback function.
find() passes 3 arguments to the callback function, which can be used in the callback function
the element
the index of the element
and the array itself
let array1 = [ "Bob", "Tom", "Harry", "Ryan"]
array1.indexof("Tom") //returns 1 which is the index of Tom
function name(element, index, array){
return (element === "Bob")
}
array1.find(name) //returns "Bob"
To locate all Elements in an array matching a certain criteria use:
Array.prototype.filter()
The method accepts 1 argument, a callback function
If the callback's return value is true, then the element is copied into a new array
returns a new array of all elements that satisfy the callback function
let array1 = [1,2,3,4,5]
function greater(element){
return num > 3; }
let array2 = array1.filter(greater)
// array2 = [4,5]
To alter each element in an array, we can use the:
Array.prototype.map()
takes a callback function as an argument
returns a new array of the altered elements
the new array is the same length as the original as nothing is filtered out, just the elements have been transformed based on the call-back function
let array1 = [1,2,3,4,5]
function double(num){
return num * 2; }
let array2 = array1.map(double);
//array2 = [2,4,6,8,10]
To aggregate or summarize information from multiple elements into one value, we use:
Array.prototype.reduce()
It takes 2 arguments, a callback function and an optional start value for the accumulator
- Within the function, the 1st argument is the accumulator and the second is the element
If no accumulator value is provided, it uses the 1st element as the start value of the accumulator and starts iteration from the second element
- BEST PRACTICE: Always include an accumulator
Using Array.prototype.forEach():
Similar to the map() method where it iterates through each object
- MAIN DIFFERENCE: It doesn't have a built-in return value like map()
It takes a callback function as its argument that allows us to return whatever we want
USAGE: Mainly when we want to mutate or change the values in the array provided. It doesn't return a new array like map()
Looping and Object Iteration
Looping is the process of executing a set of statements repeatedly until a condition is met.
Iteration is the process of executing a set of statements once for each element in a collection.
For Loop
Syntax: For(initialization, condition, iteration){
Loop Body
}
Initialization: used to initialize a counter variable
Condition: The expression evaluated before each pass through the loop
Iteration: expression executed at the end of each expression. Typically incrementing the initialization variable.
Used when you know how many times you want the loop to run
const array = [1,2,3,4,5] for (let i = 0; i < array.length; i++) { //loop body expression to perform through each loop array[i] += 1; } //array = [2,3,4,5,6]
While Loop
Similar usage to for loops
More used to compare and check conditions instead of based on a counter
syntax: while (condition) { loop body }
If the condition is true, it keeps looping. Once the condition is false, it stops the loop.
For .... of
Used when you want to iterate over an array
for (const element of array){ loop body } //performs the loop body on every element in the array
For ..... in
generally used to iterate over the properties in an object
for (const key in object) { //The keys are passed in as the elements being iterated through. if you want access to the values of the key, you can use bracket notation. object[key] //used to access the values of the keys through iteration }
- You can't use object.key while going through iteration to access the values because variables don't work. This is because it is searching for a key that actually has the value of "key," instead of the variable value stored within key. That is why you need to use bracket notation object[key] to access the value.
You can use For....in for arrays also as they are a type of object, but IT IS NOT RECOMMENDED. This is because there is no guarantee for....in will go through the array in order, which is usually required.
citations:
The Flatiron School