Iterate through array, for each item with key collect value and make new array with key and array of
来源:互联网 发布:衡水星河网络 编辑:程序博客网 时间:2024/04/29 16:31
linkword = [['people','http:mysite.org/people-appears-here'], ['people','http:mysite.org/people-appears-here-to'], ['people','http:mysite.org/people-appears-here-aswell'], ['crayons','http:mysite.org/crayons-appears-here-to'], ['crayons','http:mysite.org/crayons-appears-here-aswell'], ['boats','http:mysite.org/boats-appears-here-aswell']]
And I want to create an array(hash?) like
#=> {"people"=>["http:mysite.org/people-appears-here", "http:mysite.org/people-appears-here-to", "http:mysite.org/people-appears-here-aswell"],# "crayons"=>["http:mysite.org/crayons-appears-here-to", "http:mysite.org/crayons-appears-here-aswell"],# "boats"=>["http:mysite.org/boats-appears-here-aswell"]}
linkword.group_by(&:first).each_with_object({}) {|(k, v), h| h[k] = v.flatten.reject{|i| i == k } }
#=> {"people"=>["http:mysite.org/people-appears-here", "http:mysite.org/people-appears-here-to", "http:mysite.org/people-appears-here-aswell"],# "crayons"=>["http:mysite.org/crayons-appears-here-to", "http:mysite.org/crayons-appears-here-aswell"],# "boats"=>["http:mysite.org/boats-appears-here-aswell"]}
0 0
- Iterate through array, for each item with key collect value and make new array with key and array of
- Joining an array of keys to a hash with key value pairs like excel vlookup
- ruby merge array of hashes based on the key and order it by number of key/value pair
- How to sort an array of hashes into hashes with multiple values for a key?
- Obtain Key from a Hash of Arrays using array value
- Convert array of key value object to object of the key values (ruby)
- Select n numbers from 1 to m with all of array 'all' and none of array 'none'
- Open array parameters and array of const
- Merge array and hash in ruby if key appears in array
- Sort an Array Mixed With Integers and Strings - Ruby
- Array#collect
- The Definition of Key Value Coding and Key Value Observing
- How to combine the value of multiple hashes within an array by the same key
- Exchange the numbers of row and array of a two-dimensional array, and form a new two-dimensional ar
- Dictionary and Array value cannot be nil
- 27. Given an array and a value
- Array of arrays with PL/SQL
- Randow Id generator with array of probability
- [ACM] hdu 1671 Phone List (字典树)
- OpenSSL 常用函数——Base64编码及解码和证书操作
- 块设备驱动程序实现请求处理函数的两种方式
- Qt中加载多个txt文件
- Open SSL 常用函数——签名与验证
- Iterate through array, for each item with key collect value and make new array with key and array of
- PCA:Principle Component Analysis [3]
- BNUOJ---4033汉诺塔
- 观‘锤子手机发布会’
- iOS开发基础:OC数组对象NSArray的常用方法
- CCNA2-Cisco-Chapter1
- 黑马程序员------毕老师视频笔记第十天------面向对象(面向对象练习题下)
- Shell 之 gawk (一)基础篇
- Parencodings 1016