gpt4 book ai didi

ruby-on-rails - 如何从数组中删除空哈希

转载 作者:太空宇宙 更新时间:2023-11-03 17:06:16 24 4
gpt4 key购买 nike

如何从数组中删除空散列?

[{}, {}, :one, :two]

.compact 似乎不起作用。

最佳答案

How can I remove empty hash's from an array?

使用 reject,通过检查元素是否为哈希且为空来拒绝可枚举的元素 ({}):

p [{}, {}, :one, :two, [], [], '', nil].reject { |e| e == {} }
# [:one, :two, [], [], "", nil]

关于ruby-on-rails - 如何从数组中删除空哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58018162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com