格式化散列和数组的复杂混合物,并将最终结果作为数组输出



在修复了一个旧应用程序之后,我现在只剩下这个

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

感谢

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

最新更新