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

时间:2011-05-04 04:48:54

标签: ruby arrays

我有以下数组

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

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

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

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

20 个答案:

答案 0 :(得分:467)

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

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

您还可以使用reject!,这将修改cities。如果它拒绝某些内容,它将返回cities作为其返回值,如果没有拒绝则返回nil。如果你不小心,这可能是一个问题(感谢ninja08在评论中指出这一点)。

答案 1 :(得分:155)

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

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

答案 2 :(得分:64)

这对我有用:

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

输出:

[1, 2, "hello"]

答案 3 :(得分:52)

在我的项目中,我使用delete

cities.delete("")

答案 4 :(得分:40)

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

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

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

答案 5 :(得分:22)

最明确的

cities.delete_if(&:blank?)

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

例如:

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

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

答案 6 :(得分:21)

试试这个:

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

答案 7 :(得分:17)

使用reject

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

答案 8 :(得分:14)

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

您希望blank?使用empty?的原因是空白识别nil,空字符串和空格。例如:

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

仍会返回:

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

empty?上调用" "将返回false,您可能想要true

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

答案 9 :(得分:10)

已经有很多答案,但如果您在Rails世界中,这是另一种方法:

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

答案 10 :(得分:9)

这是实现此目的的另一种方法

我们可以将IDpresence

一起使用
select

答案 11 :(得分:8)

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

[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]

答案 12 :(得分:4)

你可以试试这个

 cities.reject!(&:empty?)

答案 13 :(得分:2)

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

答案 14 :(得分:2)

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]

这里是link to the docslink to the relative PR

也提供破坏性的变体。参见Array#compact_blank!


如果您只需要删除nil个值,

请考虑使用Ruby内置Array#compactArray#compact!方法。

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

答案 15 :(得分:2)

要删除nil值,请执行以下操作:

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

要删除空字符串:

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

要同时删除nil和空字符串:

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

答案 16 :(得分:1)

最短路[{1}}

答案 17 :(得分:0)

另一种方法:

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

答案 18 :(得分:0)

普通的红宝石:

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

答案 19 :(得分:-2)

严格更新join& split

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

结果将是:

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

请注意:这不适用于带空格的城市