我有一个这样的数组:
tweets = [
{
:user_id => 234567,
:username => "A",
:created_at => "2012-10-12 10:20:30"
},
{
:user_id => 234568,
:username => "B",
:created_at => "2012-10-12 10:20:34"
},
{
:user_id => 234569,
:username => "C",
:created_at => "2012-10-12 10:20:35"
},
{
:user_id => 234570,
:username => "D",
:created_at => "2012-10-12 10:20:40"
}
]
和另一个数组,像这样:
followers = [
{
:user_id => 234567,
:follower_ids => [234568, 56654]
},
{
:user_id => 234568,
:follower_ids => [234569, 454445]
},
{
:user_id => 234569,
:follower_ids => [234570, 56333]
},
{
:user_id => 234570,
:follower_ids => [45566, 61145]
}
]
我想将它嵌套到一个深层结构中,其中一个被制成另一个的孩子。对于制作孩子,要满足的条件是:
任何其他比其他推文更大的推文,并且如果追随者数组中的推文被认为是孩子,则
created_at
其包含在 follower_ids 列表中user_id
给定数据的预期输出如下:
arranged_tweets = [
{
:user_id => 234567,
:username => "A",
:created_at => "2012-10-12 10:20:30",
:children => [
{
:user_id => 234568,
:username => "B",
:created_at => "2012-10-12 10:20:34",
:children => [
{
:user_id => 234569,
:username => "C",
:created_at => "2012-10-12 10:20:35",
:children => [
{
:user_id => 234570,
:username => "D",
:created_at => "2012-10-12 10:20:40"
}
]
}
]
}
]
}
]