Please I need an assistance, please how to loop through an array indexes and increment the output by 1
I mean like to loop through an array indexes and for the first output of the index 0 will logout the first alphabet, and for the index 1 will log out the second alphabet
For example an array like this one [mango, banana, apple, orange, pineapple] will produce an array like this [m, a, p, n, a]
In javascript
I tried using for loop on the array and I’m just looping over the indexes not the indexes of the index
3
Answers
Use
Array.prototype.map(fn(element, index, array))
:This will loop over your fruits, and create an array with the character at the index. However, this will break if the fruit’s length is less than the index it is at.
You can use
Array.from
to create new array and pass a function that returns the character at the current index in the item.