O
O
Ord EO2018-12-03 13:47:04
ruby
Ord EO, 2018-12-03 13:47:04

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},
]

It is necessary to find the value of value by the key value, and the algorithm should be the most optimal, who can tell in which direction to move?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Blokhin, 2018-12-03
@TITnet

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 question

Ask a Question

731 491 924 answers to any question