0

I have array of arrays like this:

status = [
   ["Deleted", "deleted", 0],
   ["In planning", "planning", 1],
   ["In approval", "approval", 2]
]

How do I transform it to an array of hashes like this?

[
  {:label => "Deleted", :value => "deleted"},
  {:label => "In planning", :value => "planning"},
  {:label => "In approval", :value => "approval"}
]

So far I tried:

status.each do |s| label: s[0], value: s[1] end

However, I don't get back an array of hashes. If I do:

puts s[0], value: s[1]

I see only hashes in my console:

{:label => "Deleted", :value => "deleted"}
{:label => "In planning", :value => "planning"}
{:label => "In approval", :value => "approval"}

I believe I somehow would need to add those hashes to the array.

2
  • You can try status.map { |s| {label: s[0], value: s[1]} } Commented Jan 25, 2018 at 5:30
  • @BlueSmith Thank you, it works :) I tried similar as well, but without .map Commented Jan 25, 2018 at 5:39

2 Answers 2

2

Try to the following:

status.map { |label, value, _| { label: label, value: value } }
Sign up to request clarification or add additional context in comments.

Comments

1
status.map{ |s| Hash[label: s[0], value: s[1]] }
# => [{:label=>"Deleted", :value=>"deleted"}, {:label=>"In planning", :value=>"planning"}, {:label=>"In approval", :value=>"approval"}] 

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.