How do I get unique elements in this array?

前端 未结 6 1054
时光取名叫无心
时光取名叫无心 2021-01-30 06:11

Using Mongoid. Unfortunately, Mongoid does not allow for selecting unique / distinct! Have gotten these results. As you can see, there are 7 results. If you look carefully (at

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 07:10

    This should work for you:

    Consider Table1 has a column by the name of activity which may have the same value in more than one record. This is how you will extract ONLY the unique entries of activity field within Table1.

    #An array of multiple data entries
    @table1 = Table1.find(:all) 
    
    #extracts **activity** for each entry in the array @table1, and returns only the ones which are unique 
    
    @unique_activities = @table1.map{|t| t.activity}.uniq 
    

提交回复
热议问题