从散列中删除nil值

cod*_*r05 1 ruby hash json

我希望从散列中删除nil有价值的键.article是存储每篇文章的类,并且attributes方法将文章存储为散列.

预期结果:

{"articles":[{"results":[{"author":null,"title":"Former bar manager jailed for preying on homeless 14-year-old girl","summary":"<p><img src=\"http://images.theage.com.au/2015/08/24/6790912/Thumbnail999662740gisd08image.related.thumbnail.320x214.gj68pg.png1440386418031.jpg-90x60.jpg\" width=\"90\" height=\"60\" style=\"float:left;margin:4px;border:0px\"/></p>A man who preyed on a 14-year-old girl he came across living on the streets of&#160;Wodonga has been jailed for nine months.","images":null,"source":null,"date":"Mon, 24 Aug 2015 03:20:21 +0000","guid":"<guid isPermaLink=\"false\">gj68pg</guid>","link":"http://www.theage.com.au/victoria/former-bar-manager-jailed-for-preying-on-homeless-14yearold-girl-20150824-gj68pg.html","section":null,"item_type":null,"updated_date":null,"created_date":null,"material_type_facet":null,"abstract":null,"byline":null,"kicker":null}]}]}
Run Code Online (Sandbox Code Playgroud)

希望从上面的输出中删除空值.

def attributes
  hash = {
    "author" => @author,
    "title" => @title,
    "summary" => @summary,
    "images" => @images,
    "source" => @source,
    "date" => @date
  }
  hash = {}
  count = 0
  article.attributes.each do |key,value|
    if value == nil
      hash[count] = article.attributes.delete(key)
      count += 1
    end
  end
  hash.to_json
Run Code Online (Sandbox Code Playgroud)

结果如下:

{"0":null,"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null,"8":null,"9":null,"10":null}
Run Code Online (Sandbox Code Playgroud)

Mat*_*ttD 10

如果您使用的是 Ruby >= 2.4.6,则可以使用compact.

https://apidock.com/ruby/v2_4_6/Hash/compact

例子:

hash = {:foo => nil, :bar => "bar"}
=> {:foo=>nil, :bar=>"bar"}
hash.compact
=> {:bar=>"bar"}
hash
=> {:foo=>nil, :bar=>"bar"}
Run Code Online (Sandbox Code Playgroud)

还有compact!哪个删除 nil 值,如果哈希中没有值为零(从版本> = 2.5.5),则删除 nil。

https://apidock.com/ruby/v2_4_6/Hash/compact%21

例子:

hash
=> {:foo=>nil, :bar=>"bar"}
hash.compact!
=> {:bar=>"bar"}
hash
=> {:bar=>"bar"}
hash.compact!
=> nil
Run Code Online (Sandbox Code Playgroud)


Mys*_*yst 8

尝试怎么样:

hash = article.attributes.select {|k, v| v }
Run Code Online (Sandbox Code Playgroud)

如果值为falsenil,则将忽略该属性.

如果你想保留假值而只消除nil,你可以运行:

hash = article.attributes.select {|k, v| !v.nil? }
Run Code Online (Sandbox Code Playgroud)


Abo*_*abi 6

您可以nil从哈希(即“ h”)中删除所有值:

h.delete_if { |k, v| v.nil? }
Run Code Online (Sandbox Code Playgroud)

并且您也可以使用该值删除空值:

h.delete_if { |k, v| v.nil? || v.empty? }
Run Code Online (Sandbox Code Playgroud)


pea*_*eak 0

给定的输入是有效的 JSON,并且问题具有 JSON 标签,因此我想为递归消除 JSON 对象中的键的问题提供通用解决方案,无论它们出现在何处,也无论输入 JSON 是什么。

该解决方案是用一种新的面向 JSON 的编程语言 jq 编写的,但即使您不能使用 jq,该解决方案也非常简洁和优雅,以至于它可能会建议您使用您选择的语言来实现一般问题。

这是——一行:

walk( if type == "object" then with_entries( select(.value != null) ) else . end)
Run Code Online (Sandbox Code Playgroud)

这以 jq 版本 1.5 为前提(请参阅https://stedolan.github.io/jq/)。

如果您有旧版本的 jq,则可以轻松添加 walk/1 的定义。(参见例如使用 jq 在 JSON 结构中更深入地转换键的名称