![](/img/trans.png)
[英]Compare a string value to an array and if it matches get the index of the array item
[英]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.