Home > Net >  add new items to multidimensional Ruby hash in loop
add new items to multidimensional Ruby hash in loop

Time:08-22

Migrating my code to Ruby stuck on the updating this hash with new items inside the multidimensional hash>customers>image>

bannerhash = {
  "stooge": "larry",
  "toppings": [],
  "customers": [
    {
      "id": 1,
      "alt": "Image seo text",
      "image": {
        "label": "gid:///28663588552955",
        "image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside-table_2000x.jpg?v=1637223489"
      },
      "mainsize": 100
    },
    {
      "id": 2,
      "alt": "Image seo text",
      "image": {
        "label": "gid:///28663588487419",
        "image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/bed-side-table_2000x.jpg?v=1637223489"
      },
      "mainsize": 100
    },
    {
      "id": 3,
      "alt": "image",
      "themeposition": "388506648827/posA",
      "image": {
        "label": "gid:///28663588585723",
        "image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/sunlight-creeps-through-a-bright-living-room_2000x.jpg?v=1637223489"
      },
      "mainsize": 87
    },
    {
      "id": 4,
      "alt": "short width",
      "themeposition": "home",
      "mainsize": 70,
      "image": {
        "label": "gid:///28663588454651",
        "image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/modern-and-stylish-design_2000x.jpg?v=1637223489"
      },
      "quickcss": "@import url(\'https://fonts.googleapis.com/css2?family=Comic Neue:wght@300&display=swap\');\n.wrapper-4 .--description {font-family: \'Comic Neue\', cursive; }"
    }
  ]
}

Whant to update in the loop and the add the "newitem" key with value inside the "image" like

{
  "stooge": "larry",
  "toppings": [],
  "customers": [
    {
      "id": 1,
      "alt": "Image seo text",
      "image": {
        "label": "gid:///28663588552955",
        "image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside-table_2000x.jpg?v=1637223489",
        "newitem": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside-table_3300x.jpg?v=1637223489"
      },
      "mainsize": 100
    },
    {
      "id": 2,
      "alt": "Image seo text",
      "image": {
        "label": "gid:///28663588487419",
        "image": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/bed-side-table_2000x.jpg?v=1637223489",
        "newitem": "https://cdn.doma.com/s/files/1/0611/2064/3323/files/bed-side-table_3300x.jpg?v=1637223489"
      },
      "mainsize": 100
    }
  ]
}

My workaround code where I am stuck on how ruby update hash inside the each loop. First I catch the inner hash with the "customers" key and know the index and value however stuck on how to add "newitem" inside the "customers" "image" hash

if bannerhash.has_key?(:customers)
  puts "found"
  bannerhash.each_with_index do |(key, value), index|
    if key == :customers
      puts "index: #{index} | key: #{key} | value: #{value}"     
      # STACK Here bannerhash[key].each
    end 
  end
else
  puts "banners not found"
end

CodePudding user response:

This seems pretty straightforward. No need to loop over all of the key/value pairs looking for :customers when we can access it directly.

if bannerhash.has_key?(:customers)
  bannerhash[:customers].each { |h| 
    h[:image][:newitem] = "https://cdn.doma.com/s/files/1/0611/2064/3323/files/yellow-pillow-bedside- table_3300x.jpg?v=1637223489"
  }
else
  puts "banners not found"
end 
  • Related