繁体   English   中英

获取与数组中条件匹配的最后一个值的索引

[英]Get Index of last value that matches a condition in an array

我想获取数组中最后一个nil的索引。 对于以下数组:

["Pending Verification", "Pending Verification", nil, nil]

指数应该是3 或者在这种情况下:

[nil, "Pending Verification", nil, "Bla"]

它应该是2

我怎样才能在 ruby​​ 中做到这一点?

你可以做

a = [nil, "Pending Verification", nil, "Bla"] 
a.rindex { |x| x.nil? }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM