I want to start iterating over an array from a specific index. How can I do that?
myj.each do |temp|
...
end
To loop through an array backward using the forEach method, we have to reverse the array. To avoid modifying the original array, first create a copy of the array, reverse the copy, and then use forEach on it. The array copy can be done using slicing or ES6 Spread operator.
For Loop to Traverse Arrays. We can use iteration with a for loop to visit each element of an array. This is called traversing the array. Just start the index at 0 and loop while the index is less than the length of the array.
Do the following:
your_array[your_index..-1].each do |temp|
###
end
More idiomatic would be to use Enumerable#drop
:
myj.drop(index).each do |temp|
###
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With