1

经过大量修复旧应用程序后,我现在只剩下这个了

@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"]]]

谢谢

4

2 回答 2

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"]]]
于 2013-07-05T20:08:24.477 回答
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
于 2013-07-05T20:53:00.247 回答