将对象移动到ruby中的父对象

时间:2016-07-12 18:37:56

标签: arrays ruby object

我有一个像这样的对象数组:

[
  {
   "id": 2,
   "title": "LA COPA",
   "parent_menu_id": nil
  },
  {
   "id": 3,
   "title": "CALENDARIO",
   "parent_menu_id": nil
  },
  {
   "id": 5,
   "title": "Torneo",
   "parent_menu_id": 2
  },
  {
   "id": 6,
   "title": "Nice",
   "parent_menu_id": 2
  }
]

这是菜单的结构。

每个对象都是一个菜单项。

如果键“parent_menu_id”为nil,则表示该项目是父菜单。

如果它有价值,那就是孩子。 I.E第三项表示项目 id:5是项目ID的子项:2。

这是所需的输出:

[
  {
   "id": 2,
   "title": "LA COPA",
   "active": true,
   "parent_menu_id": nil,
   "submenus":[
     {
       "id": 5,
       "title": "Torneo",
       "active": true,
       "parent_menu_id": 2
     },
     {
       "id": 6,
       "title": "Nice",
       "parent_menu_id": 2
     }
   ]
  },
  {
   "id": 3,
   "title": "CALENDARIO",
   "active": true,
   "parent_menu_id": nil
  }
]

我知道算法:

  1. 如果pareny_menu_id与nill不同,则搜索密钥id == parent_menu_id

  2. 如果子菜单键不存在,请创建它。

  3. 将子项目移动到父项目。

  4. 然后从基本位置删除子项目。

  5. 但我不确定使用的方法..

    有什么想法吗?

    干杯!

2 个答案:

答案 0 :(得分:0)

#!ruby

require 'pp'

objects = [
  {
    "id"             => 2,
    "title"          => "LA COPA",
    "parent_menu_id" => nil
  },
  {
    "id"             => 3,
    "title"          => "CALENDARIO",
    "parent_menu_id" => nil
  },
  {
    "id"             => 5,
    "title"          => "Torneo",
    "parent_menu_id" => 2
  },
  {
    "id"             => 6,
    "title"          => "Nice",
    "parent_menu_id" => 2
  }
]

objects.collect! do |child|
  if not child["parent_menu_id"].nil? then
    parents = objects.select{|o| o["id"] == child["parent_menu_id"]}
    parents.each do |pr|
      pr["submenus"] ||= []
      pr["submenus"] << child
    end
  end
  child
end.select!{|o| o["parent_menu_id"].nil? }
pp objects

输出:

[{"id"=>2,
  "title"=>"LA COPA",
  "parent_menu_id"=>nil,
  "submenus"=>
   [{"id"=>5, "title"=>"Torneo", "parent_menu_id"=>2},
    {"id"=>6, "title"=>"Nice", "parent_menu_id"=>2}]},
 {"id"=>3, "title"=>"CALENDARIO", "parent_menu_id"=>nil}]

答案 1 :(得分:0)

arr = [
  {
   id: 2,
   title: "LA COPA",
   parent_menu_id: nil
  },
  {
    id: 3,
    title: "CALENDARIO",
    parent_menu_id: nil
  },
  {
    id: 5,
    title: "Torneo",
    parent_menu_id: 2
  },
  {
    id: 6,
    title: "Nice",
    parent_menu_id: 2
  }
]

parents, children = arr.partition { |g| g[:parent_menu_id].nil? }
  #= [[{:id=>2, :title=>"LA COPA", :parent_menu_id=>nil},
  #     {:id=>3, :title=>"CALENDARIO", :parent_menu_id=>nil}],
  #    [{:id=>5, :title=>"Torneo", :parent_menu_id=>2},
  #     {:id=>6, :title=>"Nice", :parent_menu_id=>2}]]

children.each do |child|
  parent_id = child[:parent_menu_id]
  parent = parents.find { |p| p[:id] == parent_id }
  if parent.key?(:submenus)
    parent[:submenus] << child
  else
    parent[:submenus] = [child.merge(active: true)]
  end
end

parents
  #=> [{:id=>2, :title=>"LA COPA", :parent_menu_id=>nil,
  #     :submenus=>[{:id=>5, :title=>"Torneo", :parent_menu_id=>2, :active=>true},
  #                 {:id=>6, :title=>"Nice", :parent_menu_id=>2}]},
  #    {:id=>3, :title=>"CALENDARIO", :parent_menu_id=>nil}] 

注意:

  • "id": 2是多余的。只需使用id: 2即可。当符号由多个单词组成时,您只需要引号。
  • 我假设键值对active: true仅出现在:submenus的值(数组)的第一个元素中。