O
O
Ord EO2019-09-10 02:00:46
ruby
Ord EO, 2019-09-10 02:00:46

How to add elements from a hash to an array?

As a means of pure rub from a hash of this kind

row = {lk: {id: 181, name: "Client_name"}, lpp: {id: 123, name: "Client_name2"}}

Get an array of ids, so that in the end it would be like this. [181,123]
Moreover, the hash length can be different, and it can even be empty, in which case an empty array should be returned.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Mokevnin, 2019-09-10
@OrdeO

row = {lk: {id: 181, name: "Client_name"}, lpp: {id: 123, name: "Client_name2" }}
row.map{ |k, v| v[:id] }

Higher-order functions are the head of everything. https://ruby-doc.org/core-2.6.3/Enumerable.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question