How can I delete one element from an array by value

前端 未结 15 1057
独厮守ぢ
独厮守ぢ 2020-12-04 05:16

I have an array of elements in Ruby

[2,4,6,3,8]

I need to remove elements with value 3 for example

How do I do that?<

15条回答
  •  臣服心动
    2020-12-04 06:00

    Assuming you want to delete 3 by value at multiple places in an array, I think the ruby way to do this task would be to use the delete_if method:

    [2,4,6,3,8,3].delete_if {|x| x == 3 } 
    

    You can also use delete_if in removing elements in the scenario of 'array of arrays'.

    Hope this resolves your query

提交回复
热议问题