Ruby on rails 如何在Ruby中向现有哈希数组插入数组?
我有一个下面的散列映射的当前数组,我有另一个数组,我想通过匹配id插入到每个散列中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
{"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