Last updated: Feb 26, 2024
Reading timeยท3 min
If you need to iterate over a String with Index, click on the second subheading.
Use the forEach()
method to iterate over an array with access to the current
index.
The second parameter the callback function takes is the index of the element in the array.
const arr: string[] = ['bobby', 'hadz', 'com']; arr.forEach((element, index) => { // ๐๏ธ bobby 0, hadz 1, com 2 console.log(element, index); });
The function we passed to the Array.forEach method gets called with each element in the array.
The callback function gets passed the following 3 parameters:
element
in the array.index
of the element in the array.array
on which the forEach()
method was called.The forEach()
method returns undefined
, so it is used to mutate an external
variable.
map()
If you need to loop over the array with an index but return a value on each iteration, use the Array.map method instead.
const arr: string[] = ['bobby', 'hadz', 'com']; const result = arr.map((element, index) => { return element + index; }); // ๐๏ธ ['bobby0', 'hadz1', 'com2'] console.log(result);
The function we passed to the map()
method gets called with each element in
the array and gets passed the same parameters as forEach()
.
map()
method returns a new array containing the elements we returned from the callback function.for
loopAn important thing to note when using the forEach()
method is that you can't
use the break
keyword to break out of the loop.
If you have to use the break
keyword to exit the loop if a condition is met,
use a basic for
loop instead.
const arr: string[] = ['bobby', 'hadz', 'com']; for (let index = 0; index < arr.length; index++) { // ๐๏ธ bobby 0, hadz 1 console.log(arr[index], index); if (index === 1) { break; } }
A basic
for
loop is not as elegant as using forEach()
, however, it enables us to use the
break
keyword to exit the loop once a condition is met.
If you need to iterate over a string with index:
forEach()
method to iterate over the array.forEach()
method takes is the index of the current
element.const str = 'hello'; const arr: string[] = [...str]; console.log(arr); // ๐๏ธ ['h', 'e', 'l', 'l', 'o'] arr.forEach((char, index) => { // ๐๏ธ h 0, e 1, l 2, l 3, o 4 console.log(char, index); });
We used the spread syntax (...) to unpack the string into an array.
The array contains all of the characters in the string as elements.
The function we passed to the Array.forEach
method gets called with each
element in the array.
The callback function gets passed the following 3 parameters:
element
in the array.index
of the element in the array.array
on which we called the forEach()
method.for
loopAn important thing to note when using the forEach()
method is that you can't
use the break
keyword to break out of the loop.
If you have to use the break
keyword to break out of the loop if a condition
is met, use a basic for
loop instead.
const str = 'hello'; for (let index = 0; index < str.length; index++) { // ๐๏ธ h 0, e 1, l 2 console.log(str[index], index); if (index === 2) { break; } }
The break
keyword enables us to exit the loop early and is not supported when
using the forEach()
method.
Other than that, the for
loop is a little more verbose and is used less often
in the real world as the language offers more abstractions on top of it.