How to remove unwanted ones from an array?

So, I have an array that looks like this.

["Enter Sandman", "One", "Nothing Else Matters", "Master of Puppets", "The Unforgiven", "The Day That Never Comes", "For Whom the Bell Tolls", "Fade to Black", "Sad But True", "Wherever I May Roam", "Turn the Page", "I Disappear", "Fuel", "Cyanide", "Seek & Destroy", "Whiskey In the Jar", "All Nightmare Long", "Battery", "Welcome Home (Sanitarium)", "The Unforgiven III", "The Unforgiven II", "King Nothing", "Ride the Lightning", "No Leaf Clover", "Until It Sleeps", "...And Justice for All", "Blackened", "The Memory Remains", "Hero of the Day", "The Four Horsemen", "Orion", "Creeping Death", "St. Anger", "Harvester of Sorrow", "Don't Tread on Me", "Broken, Beat & Scarred", "Disposable Heroes", "Fight Fire With Fire", "The End of the Line", "Trapped Under Ice", "Of Wolf and Man", "Whiplash", "My Apocalypse", "Suicide & Redemption", "The Shortest Straw", "Tuesday Gone"] 

This array is created by this command.

 artists = search_object.map{|x| x["trackName"]}.uniq.delete_if {|x| x == nil} 

this works well, but I need to filter out a few more elements. The user enters a text box, and as I enter, I need to narrow down the results. For example, if the user types the line "Questions", I need to take out items that do not have this name or name. Thus, he unfolds before the "Nothing Else Matters". If the user enters the letter "a", then all the others in the array that do not have "a" will be deleted.

they will enter with the parameters [: text]

I did it and it worked, but maybe there is a cleaner way

  query = params[:term] artists = search_object.map{|x| x["trackName"]}.uniq.delete_if {|x| x == nil} filtered = [] artists.each do |artist| filtered << artist if artist.include?(query) end 
+4
source share
4 answers

quick ruby ​​option:

 albums = ["hello kitty", "bad day", "all is good", "day is okay"] def filter_word_in(word,array) array.delete_if { |data| !data.match(word) } return array end result1 = filter_word_in("y", albums) puts result1.inspect # => ["hello kitty", "bad day", "day is okay"] result2 = filter_word_in("ay", result1) puts result2.inspect # => ["bad day", "day is okay"] result3 = filter_word_in("day", result2) puts result3.inspect # => ["bad day", "day is okay"] result4 = filter_word_in("day i",result3) puts result4.inspect # => ["day is okay"] 

As you can see in this code: we just save our result in variables. So where can we store our data on rails? You can use user_model for this or just store this data in memory.

Create something like this:

 class UserSongMemory attr_accessor :memory def initialize @memory = [] end def push(id, data) @memory << {id => data} end def pop(id) @memory.delete_if {|obj| obj.id == id} end end user_memory = UserSongMemory.new user_memory.add(@user.id, params[:inputed_string]) # after our calculations user.pop(@user.id) 

I prefer to store state memory in a class, but remember to clear the data of this class

+6
source

I would do this:

 term, fname = params[:term], "trackName" filtered = search_object.map {|x| x[fname] if x[fname].match(term) }.compact.uniq 

This approach eliminates the need for two cycles, one for collection and the other for choice. The uniq and compact methods exist according to your requirement.

+1
source

The Array class offers you a reject method to remove unwanted elements from your array.

0
source

Here is a slightly different approach and my reasoning.

Cause:
I am presenting a list on a web page with a text box that allows you to type a substring of the desired selection to filter to this element. So my assumption is that your users will ONLY ever print a substring. These are not experienced users who will match the regular expression for the desired track. Just using select and include? , you can limit this power of regular expression, as people have suggested, without additional complexity. Regex is a bit like using a machine gun to kill a fly in this example.

The code:
#I renamed the array, since it was a list of songs, not artists
songs.select {|s| s.upcase.include?(query.upcase)}
You can leave the upcase off if you want the request to be case sensitive.

0
source

All Articles