Home > Blockchain >  Remove elements from array based on value in rails
Remove elements from array based on value in rails

Time:11-23

arr = ['Jack', 'Ross', 'Buggi', 'Mimi', 'Zolo', 'Roy']

From this array if want to remove certain values like lets say Buggi and Zolo, how can I do that so that the arr looks something like

arr = ['Jack', 'Ross', 'Mimi', 'Roy']

Buggie and Zolo are just examples, it can be any element Ross, Roy.

I want to implement this in rails.

CodePudding user response:

I suggest using arr.filter() with a block that defines the defines the items you want to keep.

CodePudding user response:

Since it's Rails, you can use Active Support's Enumerable.without (rails 5) or Enumerable.excluding (>= rails 6)

arr = ['Buggi', 'Mimi', 'Zolo', 'Roy'].without('Buggie', 'Zolo')

Edit: since this is Rails 5, you need to use without instead of excluding

  • Related