【问题标题】:Add new items to multidimensional Ruby hash in loop在循环中向多维 Ruby 哈希添加新项目
【发布时间】:2022-10-19 02:58:29
【问题描述】:

将我的代码迁移到 Ruby 时停留在使用多维散列>客户>图像>中的新项目更新此散列

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; }"
    }
  ]
}

想要在循环中更新并在“图像”中添加具有值的“newitem”键,例如

{
  "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
    }
  ]
}

我的解决方法代码被困在每个循环内如何更新 ruby​​ 哈希。首先,我用“customers”键捕获内部散列并知道索引和值,但仍停留在如何在“customers”“image”散列中添加“newitem”

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

【问题讨论】:

  • newitem 是否与图像相同,只是 x 后缀不同(2000x => 3300x)?
  • 您实际上并没有使用该代码修改或创建新对象,您可以显示到目前为止的代码吗?
  • 首先我想添加新项目然后修改它

标签: ruby multidimensional-array


【解决方案1】:

这似乎很简单。当我们可以直接访问 :customers 时,无需遍历所有键/值对。

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 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多