简体   繁体   中英

Retrieve specific values from hashes within an array ruby rails

Noob question here!

I have an array with hashes that looks like this:

arr = [{id: 1, name: "Pedro"},{id: 2, name: "Pablo"}]

and want to have an array like this:

ids = [1,2]

I looked into using map or select like this:

ids = arr.each.select{|k,v| "id"==k}

But I can't figure it out.

请尝试以下方法:

ids = arr.map { |x| x[:id] }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM