0

我有一个以下哈希映射的当前数组,我有另一个数组,我想通过匹配 id 来插入每个哈希。

 {"url"=>"http://ubuntu64:1990/getpages",
  "id"=>"32794",
  "version"=>"2",
  "title"=>"Creating a page",
  "space"=>"test",
  "parentId"=>"32782",
  "permissions"=>"0"}

另一个数组我想根据 id 添加 'imageurl' 键/值,所以类似于 (if id == id insert 'imageurl'/'someurl.jpg}

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

1 回答 1

2
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

应该做的伎俩......也许有一个更聪明的方法来做到这一点

于 2012-08-02T14:56:53.427 回答