Suppose I have an array like this:
starting_array = [{key1: 'someKey1Value', key2: 'someKey2Value'}, {key1: 'anotherKey1Value', key2: 'anotherKey2Value'}]
I want to end up with this:
desired_array = ['someKey2Value', 'anotherKey2Value']
Whats the best way to extract all the values for key2 into a separate array?
使用Array#map
:-
starting_array.map { |hash| hash[:key2] }
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.