Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Ruby中向现有哈希数组插入数组?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在Ruby中向现有哈希数组插入数组?

Ruby on rails 如何在Ruby中向现有哈希数组插入数组?,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个下面的散列映射的当前数组,我有另一个数组,我想通过匹配id插入到每个散列中 {"url"=>"http://ubuntu64:1990/getpages", "id"=>"32794", "version"=>"2", "title"=>"Creating a page", "space"=>"test", "parentId"=>"32782", "permissions"=>"0"} 另一个数组我想基于id添加'i

我有一个下面的散列映射的当前数组,我有另一个数组,我想通过匹配id插入到每个散列中

 {"url"=>"http://ubuntu64:1990/getpages",
  "id"=>"32794",
  "version"=>"2",
  "title"=>"Creating a page",
  "space"=>"test",
  "parentId"=>"32782",
  "permissions"=>"0"}
另一个数组我想基于id添加'imageurl'键/值,比如(如果id==id插入'imageurl'/'someurl.jpg}

{"id"=>"32794", "imageurl" => "someurl.jpg}

应该这样做…也许有一个更聪明的方法来做,不过谢谢Anthony,imageurl添加到大哈希数组中的部分在哪里?是一个[:imageurl]=a2[:imageurl]?记住第一个数组没有“imageurl”键。是的[:imageurl]将在
数组中包含的散列中创建键
数组
a[:imageurl]=a2[:imageurl]
将imageurl添加到第一个数组中的散列的键中,但是如果您有与id不匹配的记录,则
a[:imageurl]
将为零
array = [...] #Declare your array with the "big" hashes here
array2 = [...] #Declare your array with the hash containing the imageurl key here

array.each do |a|
  array2.each do |a2|
    if a[:id] == a2[:id]
      a[:imageurl] = a2[:imageurl]
      break #We found it
    end
  end
end