0

I have the following code. Legislators is an array of data.

testArray = Legislators.each { |legislator| legislator['name']['first']}

In powershell this would leave me with an array of the legislator's first names.

Do I need to add each legislator object to the testArray as I loop through? I feel like there's probably a shortcut...

1 Answer 1

1

Try

testArray = Legislators.map { |legislator| legislator['name']['first'] }

each only iterates over legislators and executes the block; map iterates over it and returns an array whose elements are the values of the block.

Sign up to request clarification or add additional context in comments.

Comments

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.