作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
{"Genre" => Fantasy, "Author" => "Rowling"}, "Lor-6ren">
好吧,如果我有一个散列的散列来表示这样的书:
Books =
{"Harry Potter" => {"Genre" => Fantasy, "Author" => "Rowling"},
"Lord of the Rings" => {"Genre" => Fantasy, "Author" => "Tolkien"}
...
}
有什么方法可以简洁地获得书籍散列中所有作者的数组? (如果为多本书列出了同一作者,我需要他们的名字在每本书的数组中一次,所以不必担心清除重复项)例如,我希望能够按以下方式使用它:
list_authors(insert_expression_that_returns_array_of_authors_here)
谁知道这种表情怎么写?非常感谢您收到的任何帮助。
最佳答案
获取哈希值,然后使用 Enumerable#map
从该值(哈希数组)中提取作者:
books = {
"Harry Potter" => {"Genre" => "Fantasy", "Author" => "Rowling"},
"Lord of the Rings" => {"Genre" => "Fantasy", "Author" => "Tolkien"}
}
authors = books.values.map { |h| h["Author"] }
# => ["Rowling", "Tolkien"]
关于Ruby 快速从散列的散列中创建一个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22178446/
我是一名优秀的程序员,十分优秀!