Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ruby on Rails concatenate key & values

I have the following array:

[
  {
    "name_printed_on_badge": "John",
    "is_active": "Yes",
    "is_valid": "Yes"
  },
  {
    "name_printed_on_badge": "John",
    "is_active": "Yes",
    "is_valid": "No"
  }
]

I want to learn how to concatenate each key and value pair and return an array in the output format below:

[
  [
    "name_printed_on_badge: John",
    "is_active: Yes",
    "is_valid: Yes"
  ],
  [
    "name_printed_on_badge: John",
    "is_active: Yes",
    "is_valid: No"
  ]
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

array = [
  {
    "name_printed_on_badge": "John",
    "is_active": "Yes",
    "is_valid": "Yes"
  },
  {
    "name_printed_on_badge": "John",
    "is_active": "Yes",
    "is_valid": "No"
  }
]

array.map do |hash| 
  hash.map { |key, value| "#{key}: #{value}" }
end
# [["name_printed_on_badge: John", "is_active: Yes", "is_valid: Yes"], ["name_printed_on_badge: John", "is_active: Yes", "is_valid: No"]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading