如何使用每个迭代器获取数组索引或迭代次数? [英] How to get the array index or iteration number with an each iterator?

查看:526
本文介绍了如何使用每个迭代器获取数组索引或迭代次数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用ruby中的每个数组迭代一个数组。是否有一种简单的方法可以获得迭代次数或数组索引而无需返回for循环?

I'm iterating through an array in ruby with each. Is there an easy way to get the iteration number or array index without going back to a for loop?

推荐答案

啊,得到它。

each_with_index

呜!

编辑:哎呀!

这篇关于如何使用每个迭代器获取数组索引或迭代次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆