Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove key/value pair from array of hashes

Tags:

ruby

hash

Suppose I have an array of hashes

a = [
  {'id'=>'1','imageUrl'=>'abc','name'=>'x','age'=>'20'},             
  {'id'=>'2','imageUrl'=>'efg','name'=>'y','age'=>'30'},
  {'id'=>'3','imageUrl'=>'hij','name'=>'z','age'=>'40'}
]

What can be the fastest way to remove the key 'name' and 'age' and their corresponding value from all hashes in the array?

Basically how can I remove multiple key/value pairs?

like image 352
Aditya Avatar asked Mar 05 '15 14:03

Aditya


People also ask

How to remove an item from a hash?

We can use the delete(key) method to delete an entry of a hash. It returns the associated value of the entry deleted.

What is key and value in hash?

The key is sent to a hash function that performs arithmetic operations on it. The result (commonly called the hash value or hash) is the index of the key-value pair in the hash table.


4 Answers

Nothing like benchmarking:

Collected from the above answers: and using benchmark-ips

require 'benchmark/ips'

def a
  [
    {'id'=>'1','imageUrl'=>'abc','name'=>'x'},             
    {'id'=>'2','imageUrl'=>'efg','name'=>'y'},
    {'id'=>'3','imageUrl'=>'hij','name'=>'z'}
  ]
end


Benchmark.ips do |x|
  x.report("map w/ except!") do |times|
    a.map {|o| o.except!('name') }
  end 

  x.report("each w/ except!") do |times|
    a.each {|o| o.except!('name') }
  end 



  x.report("map w/ except") do |times|
    a.map {|o| o.except('name') }
  end 

  x.report("each w/ except") do |times|
    a.each {|o| o.except('name') }
  end 



  x.report("map w/ delete") do |times|
    a.map { |h| h.delete("name") }
  end 

  x.report("each w/ delete") do |times|
    a.each { |h| h.delete("name") }
  end 


  x.compare!
end

I got the following:

Calculating -------------------------------------
      map w/ except!     8.438k i/100ms
     each w/ except!     8.439k i/100ms
       map w/ except     5.242k i/100ms
      each w/ except     5.469k i/100ms
       map w/ delete     9.840k i/100ms
      each w/ delete     9.810k i/100ms
-------------------------------------------------
      map w/ except!      1.311B (±25.3%) i/s -      2.994B
     each w/ except!      1.360B (±25.2%) i/s -      3.048B
       map w/ except    423.818M (±25.8%) i/s -      1.238B
      each w/ except    458.859M (±25.7%) i/s -      1.315B
       map w/ delete      1.955B (±24.0%) i/s -      3.982B
      each w/ delete      2.025B (±23.5%) i/s -      4.033B

Comparison:
      each w/ delete: 2024710811.4 i/s
       map w/ delete: 1955349074.3 i/s - 1.04x slower
     each w/ except!: 1360241861.3 i/s - 1.49x slower
      map w/ except!: 1311373772.5 i/s - 1.54x slower
      each w/ except: 458859254.7 i/s - 4.41x slower
       map w/ except: 423818242.2 i/s - 4.78x slower

Using a.each { |h| h.delete("name") } is the fastest (as pointed in the comment).

like image 170
Fer Avatar answered Oct 27 '22 16:10

Fer


Try the following code:

a = [
  {'id'=>'1','imageUrl'=>'abc','name'=>'x'},             
  {'id'=>'2','imageUrl'=>'efg','name'=>'y'},
  {'id'=>'3','imageUrl'=>'hij','name'=>'z'}
]

a.each { |h| h.delete("name") }

p a # => [{"id"=>"1", "imageUrl"=>"abc"}, {"id"=>"2", "imageUrl"=>"efg"}, {"id"=>"3", "imageUrl"=>"hij"}]
like image 29
Paweł Dawczak Avatar answered Oct 27 '22 16:10

Paweł Dawczak


For example use except! (or except) method:

a.map {|o| o.except!('name') }
like image 2
Maxim Avatar answered Oct 27 '22 16:10

Maxim


Iterate the Array and delete it from each hash:

a = [
     {'id'=>'1','imageUrl'=>'abc','name'=>'x'},             
     {'id'=>'2','imageUrl'=>'efg','name'=>'y'},
     {'id'=>'3','imageUrl'=>'hij','name'=>'z'}
]
 => [{"id"=>"1", "imageUrl"=>"abc", "name"=>"x"}, {"id"=>"2", "imageUrl"=>"efg", "name"=>"y"}, {"id"=>"3", "imageUrl"=>"hij", "name"=>"z"}] 

a.each do |h|
   h.delete("name")
end

 => [{"id"=>"1", "imageUrl"=>"abc"}, {"id"=>"2", "imageUrl"=>"efg"}, {"id"=>"3", "imageUrl"=>"hij"}] 
like image 2
codingbunny Avatar answered Oct 27 '22 14:10

codingbunny