0

I have a current array of the below hash map, i have another array that i would like to insert into each hash by matching on the id.

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

The other array i want to add the 'imageurl' key/value based on the id, so something like (if id == id insert 'imageurl'/'someurl.jpg}

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

1 Answer 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

Should do the trick ... maybe there's a smarter way to do it though

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that Anthony, where is the part that imageurl gets added into the big hash array? is it a[:imageurl] = a2[:imageurl]? remember that the first array does not have an 'imageurl' key.
Yeah a[:imageurl] will create the key in the hashes contained in the array array
a[:imageurl] = a2[:imageurl] will add the imageurl to key to the hash in the first array, however if you have records that don't match an id then a[:imageurl] will be nil

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.