简体   繁体   English

从哈希数组中删除键/值对

[英]Remove key/value pair from array of hashes

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? 从数组中的所有哈希中删除键“name”和“age”及其对应值的最快方法是什么?

Basically how can I remove multiple key/value pairs? 基本上我该如何删除多个键/值对?

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

Nothing like benchmarking: 什么都不喜欢基准测试:

Collected from the above answers: and using benchmark-ips 从以上答案中收集:并使用基准测试 - 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") } 使用a.each { |h| h.delete("name") } a.each { |h| h.delete("name") } is the fastest (as pointed in the comment). a.each { |h| h.delete("name") }是最快的(如评论中所指出的)。

For example use except! 例如使用except! (or except ) method: (或除外 )方法:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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