【问题标题】:Compare three arrays of hashes and get the result without duplicates in ruby?比较三个哈希数组并在 ruby​​ 中得到没有重复的结果?
【发布时间】:2014-07-02 12:27:59
【问题描述】:

我正在使用fql gem 从 facebook 检索数据。原始的哈希数组是这样的。 Here。当我比较这三个哈希数组时,我想以这种方式获得最终结果:

{
      "photo" => [
        [0] {
                   "owner" : "1105762436",
                 "src_big" : "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xap1/t31.0-8/q71/s720x720/10273283_10203050474118531_5420466436365792507_o.jpg",
                 "caption" : "Rings...!!\n\nView Full Screen.",
                 "created" : 1398953040,
                "modified" : 1398953354,
               "like_info" : {
                  "can_like" : true,
                "like_count" : 22,
                "user_likes" : true
            },
            "comment_info" : {
                  "can_comment" : true,
                "comment_count" : 2,
                "comment_order" : "chronological"
            },
               "object_id" : "10203050474118531",
                     "pid" : "4749213500839034982"
        }
    ],
    "comment" => [
        [0] {
                 "text" : "Wow",
            "text_tags" : [],
                 "time" : 1398972853,
                "likes" : 1,
               "fromid" : "100001012753267",
            "object_id" : "10203050474118531"
        },
        [1] {
                 "text" : "Woww..",
            "text_tags" : [],
                 "time" : 1399059923,
                "likes" : 0,
               "fromid" : "100003167704574",
            "object_id" : "10203050474118531"
        }
    ],
     "users" =>[
         [0] {
                  "id": "1105762436",
                "name": "Nilanjan Joshi",
            "username": "NilaNJan219"
         },
         [1] {
                  "id": "1105762436",
                "name": "Ashish Joshi",
            "username": "NilaNJan219"
         }
    ]
}

这是我的尝试:

datas = File.read('source2.json')
all_data = JSON.parse(datas)
photos = all_data[0]['fql_result_set'].group_by{|x| x['object_id']}.to_a 
comments = all_data[1]['fql_result_set'].group_by{|x| x['object_id']}.to_a 
@photos_comments = []
@comments_users = []
@photo_users = []
photos.each do |a|
 comments.each do |b|
   if a.first == b.first
    @photos_comments << {'photo' => a.last, 'comment' => b.last}   
   else
    @comments_users << {'photo' => a.last, 'comment' => ''} unless @photos_comments.include? (a.last)
   end
 end
end

@photo_users = @photos_comments | @comments_users
@photo_comment_users = {photos_comments: @photo_users }

这是我得到的final result

最终数组中仍然存在重复项。我有grouped byobject id 的数组,这在照片和评论数组之间很常见。但问题在于只拍摄那些有 cmets 的照片。我不知道如何找出没有 cmets 的照片。 另外为了找出评论者的详细信息,ive users 数组,commentsusers 之间的共同属性是fromidid。我也无法理解如何获取用户详细信息。

【问题讨论】:

    标签: ruby arrays ruby-on-rails-4 hash fql.multiquery


    【解决方案1】:

    我想这就是你想要的:

    photos = all_data[0]['fql_result_set']
    comments = all_data[1]['fql_result_set'].group_by{|x| x['object_id']}
    @photo_comment_users = photos.map do |p| 
      { 'photo' => p, 'comment' => comments[p['object_id']] || '' }
    end
    

    对于每个photo,它需要所有具有相同object_idcomments,或者如果不存在,则返回''


    如果你也想连接用户,可以通过id映射,通过评论选择相关的:

    users = Hash[all_data[2]['fql_result_set'].map {|x| [x['id'], x]}]
    @photo_comment_users = photos.map do |p| 
      { 'photo' => p, 'comment' => comments[p['object_id']] || '', 
        'user' => (comments[p['object_id']] || []).map {|c| users[c['formid']]} }
    end
    

    【讨论】:

    • 哇......这就像一个魅力。现在我没有错过任何照片。但我缺少的另一部分是如何迭代用户数组。
    • 用户数组中有什么?它如何连接到照片和 cmets?
    • 实际上 users 数组与 cmets 数组通过 fromid 连接,与 users 数组中的 id 具有相同的值。看到这个pastie.org/9346738#279参考第三个数组。
    • @ahmadhamza - 你想把它放在哪里?
    • 在同一个数组@photo_cmets_users.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多