Delete key / value pair from hash array

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 could be the fastest way to remove the key "name" and "age" and their corresponding value from all hashes in the array?

How can I remove multiple key / value pairs?

+4
source share
4 answers

Try using 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"}]
+7
source

Nothing like benchmarking:

Compiled from the answers above: 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

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

+6
source

, except! ( except) :

a.map {|o| o.except!('name') }
+2

:

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"}] 
+2

All Articles