0

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?

1 Answer 1

2

Use Array#map :-

starting_array.map { |hash| hash[:key2] }
Sign up to request clarification or add additional context in comments.

1 Comment

Ah of course. Thanks. I'll accept your answer in a few minutes when I'l allowed to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.