All you need to know about loops and methods.
Traditional FOR LOOPS
const numbers = [10, 20, 30, 40, 50];
for (let i = 0; i < numbers.length; i++) {
console.log(numbers[i]);
}
//Output: 10 20 30 40 50
We all know this old fashion for-loop. I recommend not using it because there are 101 reasons to screw it up.
For-of loop,
const numbers = [10, 20, 30, 40, 50];
for (const value of numbers) {
console.log(value);
}
//Output: 10 20 30 40 50
this is easier to write,
but back draw is the whole loop that must be read through whether you like it or not! 😅
For each Loop
const numbers = [10, 20, 30, 40, 50];
numbers.forEach((value, index) => {
console.log(value); //Output: 10 20 30 40 50
console.log(index); //Output: 0 1 2 3 4
});
using for each loop you can get the value with the index as shown above
simple and easy to read
but you have to read the whole loop each time
these were easy...
now, the hard part starts
Methods for loops!
Deconstruction Array
let's say you have a huge array list, and you want the first, second and third values in a variable and rest on the array. We will need deconstruction of the array.
const numbers = [10, 20, 30, 40, 50];
const [first, second, third, ...restofThem] = numbers;
first;
second;
third;
restofThem;
As we can see from the output on the sides, this is simpler than the old-fashioned declaration and assignment of each variable.
Finding items using index
Let us say you have a dictionary of names
const names = ["Alice", "Bob", "Tiff", "Bruce", "Alice"];
so I want the index of Alice and ask the loop to search from index 1
so I want the 4 number to be returned as Alice is in the 4th index of the array
console.log(names.indexOf("Alice", 1));
now, if we type another unknown name, it will give me a -1 use indexOf
if I want to search from end of the array use names.lastIndexOf
now, I can place them in a function, too using find or findIndex
Recommended by LinkedIn
Some, Every and Include
let us say we have an array
const digits = [10, 20, 30, 40, 50];
console.log(numbers.includes(10));
include function will return true if it exists,
super simple and self-explanatory.
Now, the problem is people are confused between SOME method and EVERY method
so just repeat these sentences to remember
If some of the values are true then it will return true
console.log(digits.some((num) => num > 20));
if Every digit is matched then it is true
console.log(digits.every((num) => num > 20));
Filter
Now, Let us say you want to copy an array to another array, but under certain criteria, let's say numbers above 20 are to be copied only.
const boundTen = digits.filter((value) => value > 20);
console.log(boundTen);
the syntax looks like this
simple arrow function
The filter also takes arguments in its parameter
value, index and the value itself
Slice
Now, We just want to take a chunk of the array from the middle.
How we will can we do that?
we can use two filter one cuts the beginning and the other cuts the end.
but better to use the Slice method
just tell the index and it will extract it for you, SIMPLE!