Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I iterate on an array starting from a specific index?

Tags:

ruby

I want to start iterating over an array from a specific index. How can I do that?

myj.each do |temp| 
  ...
end
like image 208
marius_ll Avatar asked May 24 '17 07:05

marius_ll


People also ask

How do you iterate an array in reverse order?

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.

Can you use for in loops for arrays?

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.


2 Answers

Do the following:

your_array[your_index..-1].each do |temp| 
  ###
end
like image 159
Md. Farhan Memon Avatar answered Oct 28 '22 06:10

Md. Farhan Memon


More idiomatic would be to use Enumerable#drop:

myj.drop(index).each do |temp| 
  ###
end
like image 45
Aleksei Matiushkin Avatar answered Oct 28 '22 05:10

Aleksei Matiushkin