E
E
eldar_web2016-01-12 15:30:59
Ruby on Rails
eldar_web, 2016-01-12 15:30:59

How to select unique ones from a hash by parameter in RUBY?

Let's say we have a hash:

list = {[name: 'one', type: 'number'],[name: 'one', type: 'string'],[name: 'two', type: 'number']}

Here's how you can select unique entries from the list by the name parameter?
list.uniq doesn't help, since type values ​​can be different.
This is what the result should look like:
list = {[name: 'one', type: 'number'],[name: 'two', type: 'number']}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
N. Bekseitov, 2016-01-12
@eldar_web

list = [{name: 'one', type: 'number'}, {name: 'one', type: 'string'}, {name: 'two', type: 'number'}]
list.uniq {|i| i[:name] }

Maybe an array of hashes, but not vice versa.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question