经过大量修复旧应用程序后,我现在已经离开了这个
@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"]]]
由于
答案 0 :(得分:1)
@products = {:parent_products=>[["Product title", "Product body 1", "3", "user_1"], ["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"]]}
arr = @products[:parent_products].flat_map do |i|
i << @products[:child_products].select{|j| j.any? {|m| m == i[-1] || m == i[-2]}}
end
arr
# => ["Product title",
# "Product body 1",
# "3",
# "user_1",
# [["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",
# [["making a reply", "user_2", "3", "4"],
# ["yes i read it", "user_5", "5", "6"]]]
答案 1 :(得分:0)
class Product
attr_reader :description, :body, :id, :child_id
def initialize description, body, id, child_id
@description, @body, @id, @child_id = description, body, id, child_id
@children = []
end
def append child
@children << child
end
def accepts? child
child.id == self.id
end
def to_a
[description, body, id, child_id] + @children.map(&:to_a)
end
end
class ChildProduct < Product
def accepts? child
self.child_id == child.id
end
end
class ProductTransformer
def initialize products
@products = products
end
def transform
parents = @products[:parent_products].map{|e| Product.new *e}
children = @products[:child_products].map{|e| ChildProduct.new *e}
children.each do |child|
p = parents.detect{|parent| parent.accepts? child}
p.append child if p
children.each do |another|
next if another === child
child.append another if child.accepts?(another)
end
end
parents.map(&:to_a)
end
end
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"]]
}
ProductTransformer.new(products).transform