Say I have this:
[
{
:id => 34,
:votes_count => 3
},
{
:id => 2,
:votes_count => 0
},
]
How do I get the index based on id
? What I want to do is return 0
when I search for id: 34
, and 1
when I search for id: 2
. What is the most efficient way?
You can pass a block to #index
:
array.index {|h| h[:id] == 34 } # => 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With