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

时间:2022-08-31 07:43:01

I have the following array

我有下面的数组

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

I want to remove blank elements from the array and want the following result:

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

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

Is there any method like compact that will do it without loops?

有没有像compact这样的没有循环的方法?

16 个解决方案

#1


420  

There are many ways to do this, one is reject

有很多方法可以做到这一点,其中之一就是拒绝

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

You can also use reject!, which will modify cities in place. It will either return cities as its return value if it rejected something, or nil if no rejections are made. That can be a gotcha if you're not careful (thanks to ninja08 for pointing this out in the comments).

你也可以使用拒绝!,这将改变城市的位置。如果它拒绝了什么,它将返回城市作为它的返回值;如果没有拒绝,它将返回nil。如果你不小心的话,这可能是一个陷阱(感谢ninja08在评论中指出这一点)。

#2


142  

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

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

#3


48  

In my project I use delete:

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

cities.delete("")

#4


45  

Here is what works for me:

以下是对我有效的方法:

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

output:

输出:

[1, 2, "hello"]

#5


36  

When I want to tidy up an array like this I use:

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

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

This will remove all blank or nil elements.

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

#6


20  

Try this:

试试这个:

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

#7


19  

Most Explicit

cities.delete_if(&:blank?)

This will remove both nil values and empty string ("") values.

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

For example:

例如:

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

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

#8


15  

Use reject:

使用拒绝:

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

#9


14  

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

The reason you want to use blank? over empty? is that blank recognizes nil, empty strings, and white space. For example:

你想用空白的原因?在空的吗?是空格识别nil,空字符串和空格。例如:

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

would still return:

仍然会返回:

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

And calling empty? on " " will return false, which you probably want to be true.

和调用空的吗?on将返回false,您可能希望它为true。

Note: blank? is only accessible through Rails, Ruby only supports empty?.

注:空白?只能通过Rails访问,Ruby只支持空?

#10


10  

There are already a lot of answers but here is another approach if you're in the Rails world:

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

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

#11


8  

Here is one more approach to achieve this

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

we can use presence with select

我们可以使用状态选择

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

cities.select(&:presence)

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

#12


7  

Here is a solution if you have mixed types in your array:

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

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

Solution:

解决方案:

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

Output:

输出:

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

#13


4  

You can Try this

你可以试试这个

 cities.reject!(&:empty?)

#14


2  

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

#15


2  

Update with a strict with join & split

使用join和split严格进行更新

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

Result will be:

结果将是:

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

Note that: this doesn't work with a city with spaces

注意:这与有空间的城市不一样

#16


1  

Shortest way cities.select(&:present?)

最短方式cities.select(&:礼物?)

#1


420  

There are many ways to do this, one is reject

有很多方法可以做到这一点,其中之一就是拒绝

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

You can also use reject!, which will modify cities in place. It will either return cities as its return value if it rejected something, or nil if no rejections are made. That can be a gotcha if you're not careful (thanks to ninja08 for pointing this out in the comments).

你也可以使用拒绝!,这将改变城市的位置。如果它拒绝了什么,它将返回城市作为它的返回值;如果没有拒绝,它将返回nil。如果你不小心的话,这可能是一个陷阱(感谢ninja08在评论中指出这一点)。

#2


142  

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

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

#3


48  

In my project I use delete:

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

cities.delete("")

#4


45  

Here is what works for me:

以下是对我有效的方法:

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

output:

输出:

[1, 2, "hello"]

#5


36  

When I want to tidy up an array like this I use:

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

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

This will remove all blank or nil elements.

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

#6


20  

Try this:

试试这个:

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

#7


19  

Most Explicit

cities.delete_if(&:blank?)

This will remove both nil values and empty string ("") values.

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

For example:

例如:

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

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

#8


15  

Use reject:

使用拒绝:

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

#9


14  

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

The reason you want to use blank? over empty? is that blank recognizes nil, empty strings, and white space. For example:

你想用空白的原因?在空的吗?是空格识别nil,空字符串和空格。例如:

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

would still return:

仍然会返回:

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

And calling empty? on " " will return false, which you probably want to be true.

和调用空的吗?on将返回false,您可能希望它为true。

Note: blank? is only accessible through Rails, Ruby only supports empty?.

注:空白?只能通过Rails访问,Ruby只支持空?

#10


10  

There are already a lot of answers but here is another approach if you're in the Rails world:

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

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

#11


8  

Here is one more approach to achieve this

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

we can use presence with select

我们可以使用状态选择

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

cities.select(&:presence)

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

#12


7  

Here is a solution if you have mixed types in your array:

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

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

Solution:

解决方案:

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

Output:

输出:

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

#13


4  

You can Try this

你可以试试这个

 cities.reject!(&:empty?)

#14


2  

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

#15


2  

Update with a strict with join & split

使用join和split严格进行更新

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

Result will be:

结果将是:

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

Note that: this doesn't work with a city with spaces

注意:这与有空间的城市不一样

#16


1  

Shortest way cities.select(&:present?)

最短方式cities.select(&:礼物?)