2015-05-27 45 views
0

我有这样的功能:调用列表中的相同函数并返回没有重复的列表?

medIntCategory = MedicalInterventionCategory.find_by_category_text(category.category.text) 

但是现在我有一个名为类别的类别列表。

我想为每个类别执行上面的代码并获取medIntCategories的列表,但没有重复。

有没有简单的方法来做到这一点,因为我只处理整数?

简单来说

categoryList = [] 
for each category in categories do 
categoryList += MedicalInterventionCategory.find_by_category_text(category.category.text) 
end 

但随着重复检查

+1

究竟你“我只处理整数”是什么意思?你是说'MedicalInterventionCategory.find_by_category_text'返回一个整数吗? – Ajedi32

回答

1

这听起来像Array#mapArray#uniq工作:

category_list = categories.map{|category| 
    MedicalInterventionCategory.find_by_category_text(category.category.text) 
}.uniq 
0

,我认为这会工作

category_list = [] 
    categories.each do |category| 
     category_list << MedicalInterventionCategory.find_by_category_text(category.category.text).distinct 
    end 
+0

这是如何处理欺骗? –

1
@result=Array.new 
    ##assuming that it returns an array 
    medIntCategory = MedicalInterventionCategory.find_by_category_text(category.category.text) 
    ##get the first category obtained 
    @result << medIntCategory 
    if medIntCategory.present? 
     medIntCategory.each do |m| 
     ##add in same array only if not present 
     if [email protected]?(m) 
      @result << m.find_by_category_text(c.category.text) 
     end 
     end 
    ##return a unique value array 
    @result.flatten.compact.uniq unless @result.blank? 
    end 

希望它有助于

相关问题