如何从阵列中删除不需要的东西?

时间:2010-09-23 01:10:48

标签: ruby

好的,我有一个看起来像这样的数组。

["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's Gone"]

此数组由此命令生成

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

这很好但我需要过滤掉一些元素。用户将键入文本字段,因为他们键入我需要缩小结果范围。因此,例如,如果用户键入字符串“Matters”,我需要取出名称中没有的元素或名称。所以它被描述为“没有其他事情”。如果用户输入字母“a”,那么阵列中没有“a”的所有其他人都会被删除。

他们将参加params [:text]

我做了这个并且它有效,但也许有一种更清洁的方式

 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 个答案:

答案 0 :(得分:6)

快速红宝石变体是:

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"]

如何在此代码中看到:我们只是将结果保存在变量中。 那么,我们可以将数据存储在rails中? 您可以使用user_model,也可以将此数据存储在内存中。

创建如下内容:

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)

我更喜欢在Class中存储状态内存,但不要忘记清除这个类数据

答案 1 :(得分:1)

我会这样做:

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

这种方法不需要两个循环,一个用于收集和其他选择。 <{1}}和uniq方法符合您的要求。

答案 2 :(得分:0)

Array类为您提供了从您的数组中删除不需要的元素的“reject”方法。

答案 3 :(得分:0)

这是一种略有不同的方法,也是我的推理。

<强>原因: 我正在想象一个带有文本框的网页上的列表,该文本框允许您键入所需选择的子字符串以过滤到该项目。因此,我的假设是您的用户只能键入一个子字符串。这些不是高级用户,他们将正则表达式匹配他们想要的轨道。只需使用selectinclude?,就可以像人们建议的那样限制正则表达式的强大功能,而不会增加复杂性。在这个例子中,正则表达式有点像使用机枪杀死苍蝇。

<强>代码:
#I renamed the array, since it was a list of songs, not artists
songs.select {|s| s.upcase.include?(query.upcase)}
如果您希望查询区分大小写,可以将upcase关闭