经过大量修复旧应用程序后,我现在只剩下这个了
@products = {:parent_products=>[["Product title", "Product body 1", "3", "user1"], ["Product title 2", "Product body 2", "5", "user_2"]], :child_products=>[["making a reply", "user_2", "3", "4"], ["yes i read it", "user_5", "5", "6"], ["me too indeed. hurray", "user_1", "4", "7"], ["great to see this", "user_7", "3", "8"]]}
现在我不知道该怎么做是-
- 以这样的方式格式化
@products[:child_products]
,如果可以说@products[:child_products][2][2]
包含在任何元素中的任何元素中,并且内部索引是[3]
它应该附加到该数组的最后一个索引,作为数组本身。
因此,例如,从上面给定的哈希中,我们可以看到
的值@products[:child_products][2][2]
确实彼此喜欢,在@products[:child_products][0][3]
(即整数 4)。由于它正在满足true
新数组现在应该看起来像
["making a reply","user_2", "3", "4", ["me too indeed. hurray","user_1", "4", "7"]]
现在注意:以上只是一个例子。换句话说,在@products[:child_products]
搜索中应该以它看起来的方式进行搜索
@products[:child_products][<any index>][2] inside @products[:child_products][<any index>][3]
希望到目前为止我是有道理的。
- 继续前进,如果条件不满足甚至不满足(如第一点所述)并且里面的数组
@products[:child_products]
已经重新排列,它现在应该运行另一个逻辑,其中
@products[:child_products][<any index>][2] should look for @product[:parent_products][<any index>][2]
一旦满足条件,它应该将整个数组附加到数组中@product[:parent_products]
因此,例如@product[:child_products][1][2]
匹配@product[:parent_products][1][2]
(即 5),那么内部的新数组集(相应索引位置的)@product[:parent_products]
应该看起来像
["Product title 2", "Product body 2", "5", "user_2",["yes i read it", "user_5", "5", "6"]]
就这样。
我尽力确保我清楚地说明输出。如果您有任何问题,请询问。
更新:我看到上面的内容造成了混乱。所以这里是我所拥有的和我想要的快速浏览
我有的
@products = {:parent_products=>[["Product title", "Product body 1", "3", "user1"], ["Product title 2", "Product body 2", "5", "user_2"]], :child_products=>[["making a reply", "user_2", "3", "4"], ["yes i read it", "user_5", "5", "6"], ["me too indeed. hurray", "user_1", "4", "7"], ["great to see this", "user_7", "3", "8"]]}
我想要的最终输出是一个带有格式化数据的数组
[["Product title", "Product body 1", "3", "user1", ["making a reply", "user_2", "3", "4", ["me too indeed. hurray", "user_1", "4", "7"]], ["great to see this", "user_7", "3", "8"] ], ["Product title 2", "Product body 2", "5", "user_2", ["yes i read it", "user_5", "5", "6"]]]
谢谢