Reputation: 21
In a .rb file I am using result = JSON.parse(res.body)['data']['results']
and get
[
{"suggestion":"Lineman","id":"49.10526"},
{"suggestion":"Linguist","id":"27.10195"},
{"suggestion":"Librarian","id":"25.47"},
{"suggestion":"Lifeguard","id":"33.39"},
{"suggestion":"Line Cook","id":"35.30125"},
{"suggestion":"Life Coach","id":"21.209"},
{"suggestion":"Life Guard","id":"33.1001"}
]
now I want an array like
[
"Lineman",
"Linguist",
"Librarian",
"Lifeguard",
"Line Cook",
"Life Coach",
"Life Guard"
]
What should I apply to JSON.parse(res.body)['data']['results']
?
Upvotes: 0
Views: 172
Reputation: 1108
Try this:
suggestions = result.pluck(:suggestion)
# ["Lineman", "Linguist", "Librarian", "Lifeguard", "Line Cook", "Life Coach", "Life Guard"]
This plucks all the suggestion values and returns them as an array.
Upvotes: 1
Reputation: 18504
You can use Enumerable#map
:
other_result = result.map { |val| val['suggestion'] }
it returns a new array with results of applying the block to initial array elements
Upvotes: 1