简体   繁体   中英

Ruby Compare 2 Arrays of Hashes with Sets of Keys

So I have 2 arrays of hashes, which look something like this:

arr1 = [{:tag=>"ABC_0001", :bocount=>100, :bocreated=>"2017-02-24T14:57:39.549-05:00"},{:tag=>"ABC_0002", :bocount=>50, :bocreated=>"2017-02-24T14:57:41.156-05:00"},{:tag=>"ABC_0003", :bocount=>10, :bocreated=>"2017-02-24T14:57:42.903-05:00"},{:tag=>"ABC_0004", :bocount=>12, :bocreated=>"2017-02-24T14:57:44.156-05:00"}]

arr2 = [{:tag=>"ABC_0001", :bocount=>100, :bocreated=>"2017-01-24T14:57:39.549-05:00"},{:tag=>"ABC_0003", :bocount=>10, :bocreated=>"2017-01-24T14:57:42.903-05:00"},{:tag=>"ABC_0004", :bocount=>12, :bocreated=>"2017-01-24T14:57:44.156-05:00"},{:tag=>"ABC_0005", :bocount=>75, :bocreated=>"2017-01-24T14:57:41.156-05:00"}]

What I need to do, is remove the bocreated hashes from these arrays, entirely. I need to make a copy of each array (got that), and then in the new array, remove all bocreated hashes.

I have looked everywhere, but can't seem to find this exactly. Mostly about removing certain values.

I tried this:

oi_newset.each do |h|
    h.delete("#{h[:bocreated]}")
end

but when I output the array, those hashes are still there.

If anyone can help, that would be awesome.

Thanks!

try this.

oi_newset.each do |h|
  h.delete(:bocreated)
end

You need to delete the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM