Obtain Key from a Hash of Arrays using array value
来源:互联网 发布:龙江网络投诉电话多少 编辑:程序博客网 时间:2024/05/16 06:53
I have a Hash as follows:
{'a' => [title: 'ab', path: 'cd'], 'b' => [title: 'ef', path: 'gh']}
Now lets say I have one title and wish to get the key from it...
i.e. if I have 'ef' and want 'b'
h = {'a' => [title: 'ab', path: 'cd'], 'b' => [title: 'ef', path: 'gh']}h.select { |_,v| v[0][:title] == 'ef' }.keys
h = {'a' => [title: 'ab', path: 'cd'], 'b' => [title: 'ef', path: 'gh']} #=> {"a"=>[{:title=>"ab", :path=>"cd"}], "b"=>[{:title=>"ef", :path=>"gh"}]}h.each_with_object({}) { |(k,v),g| g[v.first[:title]] = k }['ef'] #=> "b"
or
h.each_with_object({}) { |(k,v),g| g[k] = v.first[:title] }.invert['ef'] #=> "b"
0 0
- Obtain Key from a Hash of Arrays using array value
- Array.forEach() and Using square brackets to obtain the value of a property
- Array.forEach() & Using square brackets to obtain the value of a property
- Array.forEach() & Using square brackets to obtain the value of a property
- Joining an array of keys to a hash with key value pairs like excel vlookup
- Array to hash of arrays
- Get a hash value using XS
- How do I sort an array of hashes by a value in the hash?
- Using libxml2 to get value of specific key
- the key specified to compute a hash value is expired
- Using Key-Value Programming
- Ruby Arrays: keep repeated values only from the end of an array of arrays
- Convert array of key value object to object of the key values (ruby)
- Get value from properties file outside a jar using Spring
- Zip together sub arrays of a 2 dimensional array
- Iterate through array, for each item with key collect value and make new array with key and array of
- How to get position of key/value in LinkedHashMap using its key
- ruby merge array of hashes based on the key and order it by number of key/value pair
- jquery+java 仿Google日历 .
- kmeans 聚类算法
- home键监听、屏蔽和模拟home键
- 设计模式--单例模式
- CCA for CRM 2011的安装和部署
- Obtain Key from a Hash of Arrays using array value
- FastJSON实现详解
- 33行找不到符号
- 崔紫娟夫君康益明
- matlab生成可执行文件
- 面试题:统计数组
- octopress+github管理个人博客
- leetcode-Clone Graph
- 命令行访问iphone的几种方式