ruby - 如何从哈希数组中提取
问题描述
people = [
{id: 101, first_name: 'John', last_name: 'Doe'},
{id: 102, first_name: 'Tom', last_name: 'Rogers'},
{id: 103, first_name: 'Bill', last_name: ''}
]
我想放一个这样的名字列表
"John Doe, Tom Rogers, Bill"
我怎样才能在红宝石中实现这一点?
解决方案
输入
people = [
{ id: 101, first_name: 'John', last_name: 'Doe' },
{ id: 102, first_name: 'Tom', last_name: 'Rogers' },
{ id: 103, first_name: 'Bill', last_name: '' }
]
代码
p people.map { |x| x[:first_name].to_s + ' ' + x[:last_name].to_s }
.map(&:strip)
.join(",")
输出
"John Doe,Tom Rogers,Bill"
推荐阅读
- c++ - 添加字符串和文字,如果可以无错误地添加字符串,顺序将如何影响
- animation - AnimationAction 如何在最后一帧停止而不在three.js中循环?
- javascript - 用于替换中间字母的正则表达式
- javascript - 输入导致链接更改的输入
- java - 有没有办法使用 Python 生成 Java 源代码的抽象语法树?
- css - 孩子 div 是否继承父母的身高?
- windows - 无法运行 git clone 或 pip install 命令
- android - StackOverflowError 与 lint
- ios - 如何在 CollectionView 中添加页脚/页眉
- vuejs2 - Vue停止列表/扩展