如何获得此数组中的唯一元素? [英] How do I get unique elements in this array?

查看:60
本文介绍了如何获得此数组中的唯一元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Mongoid.不幸的是,Mongoid 不允许选择独特的/独特的!得到了这些结果.如您所见,有 7 个结果.如果仔细看(在 user_id 处),只有 2 个用户.

<预><代码>[#<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend":4cea2fb872357e00fa000025'),artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,#<Activity _id: 4cea6c3072357e00fa000116, created_at: 2010-11-22 13:12:16 UTC, updated_at: 2010-11-22 13:12:16 UTC, action: "Attend: B'SONId", user_Id4cea2fb872357e00fa000025'),artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,#<Activity _id: 4cea6bdd72357e00fa00010d, created_at: 2010-11-22 13:10:53 UTC, updated_at: 2010-11-22 13:10:53 UTC, action: "Attend", user_id(4cea2fb872357e00fa000025'),artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,#<Activity _id: 4cea46df72357e00fa0000a4, created_at: 2010-11-22 10:33:03 UTC, updated_at: 2010-11-22 10:33:03 UTC, action: "Attend", user_id(4cea2fb872357e00fa000025'),artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,#<Activity _id: 4cea40c572357e00fa00006f, created_at: 2010-11-22 10:07:01 UTC, updated_at: 2010-11-22 10:07:01 UTC, action: "Attend(Battend", user_ObjectId)4cea2fb872357e00fa000025'),artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>,#<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id(4cea39b772357e00fa000046'),artist_id:nil,media_id:BSON::ObjectId('4cea3c8b72357e00fa00005e')>,#<Activity _id: 4cea344a72357e00fa00003f, created_at: 2010-11-22 09:13:46 UTC, updated_at: 2010-11-22 09:13:46 UTC, action: "Attend:B'SONId", user_id4cea2fb872357e00fa000025'),artist_id:nil,media_id:BSON::ObjectId('4cea306c72357e00fa000031')>]

我正在查看 this,我想我可以做一些类似的事情,这样我的数组现在看起来像这样:

<预><代码>[#<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend":4cea2fb872357e00fa000025'),artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>,#<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id(4cea39b772357e00fa000046'),artist_id:nil,media_id:BSON::ObjectId('4cea3c8b72357e00fa00005e')>]

我不关心提取的是哪种结果组合.只要我在结果集中有唯一的 user_id.有人知道如何实现吗?

解决方案

你可以只使用 uniq 方法.假设你的数组是 ary,调用:

ary.uniq{|x|x.user_id}

这将返回一个具有唯一 user_ids 的集合.

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 user_id), there are only 2 users.

[
  #<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea6c3072357e00fa000116, created_at: 2010-11-22 13:12:16 UTC, updated_at: 2010-11-22 13:12:16 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea6bdd72357e00fa00010d, created_at: 2010-11-22 13:10:53 UTC, updated_at: 2010-11-22 13:10:53 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea46df72357e00fa0000a4, created_at: 2010-11-22 10:33:03 UTC, updated_at: 2010-11-22 10:33:03 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea40c572357e00fa00006f, created_at: 2010-11-22 10:07:01 UTC, updated_at: 2010-11-22 10:07:01 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>, 
  #<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id: BSON::ObjectId('4cea39b772357e00fa000046'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>, 
  #<Activity _id: 4cea344a72357e00fa00003f, created_at: 2010-11-22 09:13:46 UTC, updated_at: 2010-11-22 09:13:46 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea306c72357e00fa000031')>
] 

I was looking at this, and was thinking I could do something similar so that my array would now look like this:

[
  #<Activity _id: 4cea6c4572357e00fa00011a, created_at: 2010-11-22 13:12:37 UTC, updated_at: 2010-11-22 13:12:37 UTC, action: "Attend", user_id: BSON::ObjectId('4cea2fb872357e00fa000025'), artist_id: nil, media_id: BSON::ObjectId('4cea447472357e00fa00009a')>, 
  #<Activity _id: 4cea3ca172357e00fa000062, created_at: 2010-11-22 09:49:21 UTC, updated_at: 2010-11-22 09:49:21 UTC, action: "Attend", user_id: BSON::ObjectId('4cea39b772357e00fa000046'), artist_id: nil, media_id: BSON::ObjectId('4cea3c8b72357e00fa00005e')>
]

I'm not concerned which combination of results are extracted. As long as I have unique user_id's in the result set. Anyone know how this can be achieved?

解决方案

You can just use the method uniq. Assuming your array is ary, call:

ary.uniq{|x| x.user_id}

and this will return a set with unique user_ids.

这篇关于如何获得此数组中的唯一元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆