RoR / Ruby 从嵌套数组中删除 nil 元素

发布于 2024-08-12 03:08:02 字数 374 浏览 4 评论 0原文

要将数组分成两个相等的部分,我会这样做,

>> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
>> a.in_groups_of( (a.size/2.0).ceil ) if a.size > 0
=> [[1, 2, 3], [4, 5, nil]]

现在我有一个嵌套数组,如果数组的大小是奇数,则它包含 nil 元素。如何从嵌套数组中删除 nil 元素?我想做类似的事情,

a.compact

但不幸的是,这不起作用,ruby 只删除第一级的 nil 元素,而不是递归地删除。 ruby 是否为这个问题提供了一些好的解决方案?

To split an array into two equal pieces I would do this,

>> a = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
>> a.in_groups_of( (a.size/2.0).ceil ) if a.size > 0
=> [[1, 2, 3], [4, 5, nil]]

Now I've got a nested array that contains nil elements if the size of the array is odd. How can I remove the nil elements from the nested arrays? I want to do something like,

a.compact

But unfortunately that doesn't work, ruby only removes nil elements on the first level and not recursively. Does ruby provide any nice solutions for this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南巷近海 2024-08-19 03:08:02

使用 Ruby 1.8.7 及更高版本,您可以执行以下操作:

a.each &:compact!
=> [[1, 2, 3], [4, 5]]

使用 Ruby 1.8.6,您必须这样做:

a.each {|s| s.compact!}

这两者都会修改 a 的内容。如果您想返回一个新数组并保留原始数组,可以使用 collect 而不是 each

# 1.8.7+:
a.collect &:compact

# 1.8.6:
a.collect {|s| s.compact}

With Ruby 1.8.7 and later you can do the following:

a.each &:compact!
=> [[1, 2, 3], [4, 5]]

With Ruby 1.8.6, you have do do this the long way:

a.each {|s| s.compact!}

Both of these will modify the contents of a. If you want to return a new array and leave the original alone, you can use collect instead of each:

# 1.8.7+:
a.collect &:compact

# 1.8.6:
a.collect {|s| s.compact}
无声无音无过去 2024-08-19 03:08:02

If you were to use the in_groups_of you can pass it false as the second argument and it will not fill in the "blanks" with nil, but truly nothing.

巾帼英雄 2024-08-19 03:08:02

除非您想永久更改a

a.map do |e|
  e.compact
end

Unless you want to permanently change a

a.map do |e|
  e.compact
end
栩栩如生 2024-08-19 03:08:02
arr_mixed=['hello', 2222, 'world', nil, [1, 2, 5, 'hi', nil],[1, 2, 3], [4,   5, nil]]

def compact_nested(array)
   arr2=[]
   array.each { |x|
   element = x.class==Array ? x.compact : x
   arr2<<element
   }
   arr2
end

p compact_nested(arr_mixed).compact #=>["hello", 2222, "world", [1, 2, 5, "hi"], [1, 2, 3], [4, 5]]
p arr_mixed #=>["hello", 2222, "world", nil, [1, 2, 5, "hi", nil], [1, 2, 3], [4, 5, nil]]
arr_mixed=['hello', 2222, 'world', nil, [1, 2, 5, 'hi', nil],[1, 2, 3], [4,   5, nil]]

def compact_nested(array)
   arr2=[]
   array.each { |x|
   element = x.class==Array ? x.compact : x
   arr2<<element
   }
   arr2
end

p compact_nested(arr_mixed).compact #=>["hello", 2222, "world", [1, 2, 5, "hi"], [1, 2, 3], [4, 5]]
p arr_mixed #=>["hello", 2222, "world", nil, [1, 2, 5, "hi", nil], [1, 2, 3], [4, 5, nil]]
羁客 2024-08-19 03:08:02
a.each {|subarray| subarray.compact!}
a.compact!

应该工作....

a.each {|subarray| subarray.compact!}
a.compact!

Should work....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文