If array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
, I want to delete a range of elements from array.
如果array = [1,2,3,4,5,6,7,8,9],我想从数组中删除一系列元素。
For example: I want to delete all elements with an index in the range 2..5
from that array, the result should be [1, 2, 7, 8, 9]
例如:我想从该数组中删除索引在2..5范围内的所有元素,结果应为[1,2,7,8,9]
Thanks in advance.
提前致谢。
5 个解决方案
#1
19
Use slice!
:
使用切片!:
Deletes the element(s) given by [...] a range.
删除[...]范围给出的元素。
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice!(2..5)
array #=> [1, 2, 7, 8, 9]
#2
2
You can try this
你可以试试这个
[1, 2, 3, 4, 5, 6, 7, 8, 9].reject.with_index{|element,index| index >= 2 && index <= 5}
=> [1, 2, 7, 8, 9]
or use delete_if
或使用delete_if
[1, 2, 3, 4, 5, 6, 7, 8, 9].delete_if.with_index{|element,index| index >= 2 && index <= 5}
=> [1, 2, 7, 8, 9]
#3
1
As Stefan posted, use slice!
to remove values located inside a certain range in the array. If what you need, however, is to remove values that are within a certain range use delete_if
.
正如Stefan发布的那样,使用切片!删除位于数组中某个范围内的值。但是,如果您需要删除某个范围内的值,请使用delete_if。
array = [9, 8, 7, 6, 5, 4, 3, 2, 1]
array.delete_if {|value| (2..5) === value }
array #=> [9, 8, 7, 6, 1]
#4
0
Delete When Range Includes Value
One of the many ways to do this is to invoke Array#delete_if with a block that checks whether each element of the Array is included in the Range with Array#include?. For example:
执行此操作的许多方法之一是使用块来调用Array#delete_if,该块检查Array的每个元素是否包含在Range with array #include中。例如:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array.delete_if { |i| (2..5).include? i }
#=> [1, 6, 7, 8, 9]
#5
-1
Delete Array Elements by Index from Range
If you are trying to delete elements by index rather than by value, one way to solve this is to iterate over your Range object, invoking Array#delete_at for each index in the range. For example:
如果您尝试按索引而不是按值删除元素,解决此问题的一种方法是迭代Range对象,为范围中的每个索引调用Array#delete_at。例如:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
(2..5).map { |i| array.delete_at i }
##=> [3, 5, 7, 9]
#1
19
Use slice!
:
使用切片!:
Deletes the element(s) given by [...] a range.
删除[...]范围给出的元素。
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array.slice!(2..5)
array #=> [1, 2, 7, 8, 9]
#2
2
You can try this
你可以试试这个
[1, 2, 3, 4, 5, 6, 7, 8, 9].reject.with_index{|element,index| index >= 2 && index <= 5}
=> [1, 2, 7, 8, 9]
or use delete_if
或使用delete_if
[1, 2, 3, 4, 5, 6, 7, 8, 9].delete_if.with_index{|element,index| index >= 2 && index <= 5}
=> [1, 2, 7, 8, 9]
#3
1
As Stefan posted, use slice!
to remove values located inside a certain range in the array. If what you need, however, is to remove values that are within a certain range use delete_if
.
正如Stefan发布的那样,使用切片!删除位于数组中某个范围内的值。但是,如果您需要删除某个范围内的值,请使用delete_if。
array = [9, 8, 7, 6, 5, 4, 3, 2, 1]
array.delete_if {|value| (2..5) === value }
array #=> [9, 8, 7, 6, 1]
#4
0
Delete When Range Includes Value
One of the many ways to do this is to invoke Array#delete_if with a block that checks whether each element of the Array is included in the Range with Array#include?. For example:
执行此操作的许多方法之一是使用块来调用Array#delete_if,该块检查Array的每个元素是否包含在Range with array #include中。例如:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
array.delete_if { |i| (2..5).include? i }
#=> [1, 6, 7, 8, 9]
#5
-1
Delete Array Elements by Index from Range
If you are trying to delete elements by index rather than by value, one way to solve this is to iterate over your Range object, invoking Array#delete_at for each index in the range. For example:
如果您尝试按索引而不是按值删除元素,解决此问题的一种方法是迭代Range对象,为范围中的每个索引调用Array#delete_at。例如:
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
(2..5).map { |i| array.delete_at i }
##=> [3, 5, 7, 9]