与条件数组的第一个元素 [英] First element of an array with condition
本文介绍了与条件数组的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有找到一个阵列会议的第一个元素一个较短的方式在一定条件比这个:
my_array [my_array.index {| X | x.some_test}]
解决方案
试试这个:
my_array.find {| X | x.some_test}
或者这里是一个快捷方式(感谢@LarsHaugseth提醒一下吧)
my_array.find(安培;:some_test)
Is there a shorter way to find the first element in an array meeting some conditions than this:
my_array[ my_array.index {|x| x.some_test} ]
解决方案
Try this:
my_array.find{|x| x.some_test }
Or here's a shortcut (thanks @LarsHaugseth for reminding about it)
my_array.find(&:some_test)
这篇关于与条件数组的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文