如何从数组中删除空白元素?

我有下面的数组

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"]

我想从数组中删除空白元素,并希望得到以下结果:

cities = ["Kathmandu", "Pokhara", "Dharan", "Butwal"]

是否有任何类似compact的方法可以在没有循环的情况下执行此操作?

253407 次浏览

有很多方法可以做到这一点,其中一个是reject

noEmptyCities = cities.reject { |c| c.empty? }

你也可以使用reject!,它会在适当的地方修改cities。如果它拒绝了一些东西,它将返回cities作为返回值,如果没有拒绝,它将返回nil。如果你不小心,这可能是一个陷阱(感谢ninja08在评论中指出这一点)。

使用reject:

>> cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"].reject{ |e| e.empty? }
=> ["Kathmandu", "Pokhara", "Dharan", "Butwal"]
 cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"].delete_if {|c| c.empty? }

试试这个:

puts ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"] - [""]

已经有很多答案了,但如果你在Rails世界里,这里有另一种方法:

 cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"].select &:present?

在我的项目中,我使用delete:

cities.delete("")
1.9.3p194 :001 > ["", "A", "B", "C", ""].reject(&:empty?)


=> ["A", "B", "C"]

如果你的数组中有混合类型,下面是一个解决方案:

[nil,"some string here","",4,3,2]

解决方案:

[nil,"some string here","",4,3,2].compact.reject{|r| r.empty? if r.class == String}

输出:

=> ["some string here", 4, 3, 2]

当我想整理一个这样的数组时,我使用:

["Kathmandu", "Pokhara", "", "Dharan", "Butwal"] - ["", nil]

这将删除所有空白或nil元素。

cities.reject! { |c| c.blank? }

你想要使用blank?而不是empty?的原因是,blank可以识别nil、空字符串和空白。例如:

cities = ["Kathmandu", "Pokhara", " ", nil, "", "Dharan", "Butwal"].reject { |c| c.blank? }

仍然会返回:

["Kathmandu", "Pokhara", "Dharan", "Butwal"]

" "上调用empty?将返回false,你可能希望它是true

注意:blank?只能通过Rails访问,Ruby只支持empty?

以下是对我有效的方法:

[1, "", 2, "hello", nil].reject(&:blank?)

输出:

[1, 2, "hello"]

你可以试试这个

 cities.reject!(&:empty?)

这里还有一种方法可以实现这一点

我们可以将presenceselect结合使用

cities = ["Kathmandu", "Pokhara", "", "Dharan", nil, "Butwal"]


cities.select(&:presence)


["Kathmandu", "Pokhara", "Dharan", "Butwal"]

最明确的

cities.delete_if(&:blank?)

这将同时删除nil值和空字符串("")值。

例如:

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal", nil]


cities.delete_if(&:blank?)
# => ["Kathmandu", "Pokhara", "Dharan", "Butwal"]

最短的方法cities.select(&:present?)

使用join &严格更新;split

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"]
cities.join(' ').split

结果将是:

["Kathmandu", "Pokhara", "Dharan", "Butwal"]

注意:这对有空间的城市不起作用

另一种方法:

> ["a","b","c","","","f","g"].keep_if{|some| some.present?}
=> ["a","b","c","f","g"]

compact_blank (Rails 6.1+)

如果你正在使用Rails(或独立的ActiveSupport),从版本6.1开始,有一个compact_blank方法从数组中删除blank值。

它在底层使用Object#blank?来确定项是否为空。

["Kathmandu", "Pokhara", "", "Dharan", nil, "Butwal"].compact_blank
# => ["Kathmandu", "Pokhara", "Dharan", "Butwal"]


[1, "", nil, 2, " ", [], {}, false, true].compact_blank
# => [1, 2, true]

这里是链接到文档链接到相关PR

破坏性的变种也是可用的。看到Array#compact_blank!


如果你有一个旧版本的Rails,检查compact_blank内部实现

反向移植并不复杂。

def compact_blank
reject(&:blank?)
end

如果你只需要删除nil值,可以考虑使用Ruby内置的Array#compactArray#compact!方法。

["a", nil, "b", nil, "c", nil].compact
# => ["a", "b", "c"]

纯Ruby:

values = [1,2,3, " ", "", "", nil] - ["", " ", nil]
puts values # [1,2,3]

删除空值:

 ['a', nil, 'b'].compact  ## o/p =>  ["a", "b"]

删除空字符串:

   ['a', 'b', ''].select{ |a| !a.empty? } ## o/p => ["a", "b"]

删除空字符串和空字符串:

['a', nil, 'b', ''].select{ |a| a.present? }  ## o/p => ["a", "b"]

更新在拒绝和拒绝!

我遇到了这个问题,并用ruby-3.0.1检查了irb控制台上的这些方法。我也检查了ruby文档,但没有提到这一点。我不确定从哪个ruby版本 变化就在那里。

.来自社区的任何帮助都是非常感激的

对于ruby-3.0.1,我们可以使用rejectreject!

cities = ["Kathmandu", "Pokhara", "", "Dharan", "Butwal"]
cities.reject{ |e| e.empty? }
=> ["Kathmandu", "Pokhara", "Dharan", "Butwal"]

或简写

cities.reject(&:empty?)
=> ["Kathmandu", "Pokhara", "Dharan", "Butwal"]

无论是否为空值,都将返回[] ?

enter image description here