Answer the question
In order to leave comments, you need to log in
What is the fastest way to find an element in an array using ruby?
There is an array like
arr = [
{key: 1, value: some_value441},
{key: 2, value: some_value212},
{key: 3, value: some_value33},
{key: 4, value: some_value44},
]
Answer the question
In order to leave comments, you need to log in
Array#find
Array#detect
items = [
{ name: :sergey, value: :qa },
{ name: :boris, value: :c_plus_plus_dev },
{ name: :vlad, value: :analytic },
{ name: :eugene, value: :marketer }
].detect { |item| item[:name] == :boris }[:value]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question