用ruby从数组创建嵌套散列(creating nested hash from array with ruby)

编程入门 行业动态 更新时间:2024-10-27 07:23:45
用ruby从数组创建嵌套散列(creating nested hash from array with ruby)

我试图重新排列数组中的数据,以散列格式,但我相当混乱使用嵌套if

样本数据

[ ["England", "London", "University College London ", "Faculty of Law"], ["England", "London", "University College London ", "Faculty of Engineering"], ["England", "London", "Imperial College London", "Faculty of Medicine"], ["England", "Manchester", "University of Manchester", "Faculty of Engineering"] ]

预期产出

{:name=>"England", :level=>1, :children=>[{:name=>"London", :level=>2, :children=>[{:name=>"University College London ", :level=>3, :children=>[{:name=>"Faculty of Law", :level=>4, :children=>[]}, {:name=>"Faculty of Engineering", :level=>4, :children=>[]}]}, {:name=>"Imperial College London", :level=>3, :children=>[{:name=>"Faculty of Engineering", :level=>4, :children=>[]}] }] }] }

希望我已经提供了明确的解释

PS。 编辑,以显示我已经尝试过的第一次,我做了散列数组,然后做这样的事情,我认为它不会混淆这么多

result = [] arr.each do |b| if result.any? {|r| r[:name] == b[:name]} if result.first[:children].any? {|r| r[:name] == b[:children].first[:name]} if result.first[:children].any?{|c| c[:children].any? {|r| r[:name] == b[:children].first[:children].first[:name] && c[:name] == b[:children].first[:name] }} if result.first[:children].any? {|r| r[:children].any? {|c| c[:children].any?{|k| k[:name] == b[:children].first[:children].first[:children].first[:name] && (c[:name] == b[:children].first[:children].first)}}} else result.first[:children].any?{|c| c[:children].any? {|r| r[:name] == b[:children].first[:children].first[:name] ; r[:children] << b[:children].first[:children].first[:children].first}} end #fourth else result.first[:children].any? {|r| r[:name] == b[:children].first[:name]; r[:children] << b[:children].first[:children].first} end else result.any? {|r| r[:name] == b[:name] ; r[:children] << b[:children].first} end else result << b end end

I'm trying to rearrange data in array to be in hash format but I pretty messed up using nested if

Sample Data

[ ["England", "London", "University College London ", "Faculty of Law"], ["England", "London", "University College London ", "Faculty of Engineering"], ["England", "London", "Imperial College London", "Faculty of Medicine"], ["England", "Manchester", "University of Manchester", "Faculty of Engineering"] ]

Expected Output

{:name=>"England", :level=>1, :children=>[{:name=>"London", :level=>2, :children=>[{:name=>"University College London ", :level=>3, :children=>[{:name=>"Faculty of Law", :level=>4, :children=>[]}, {:name=>"Faculty of Engineering", :level=>4, :children=>[]}]}, {:name=>"Imperial College London", :level=>3, :children=>[{:name=>"Faculty of Engineering", :level=>4, :children=>[]}] }] }] }

hope I have provide clear explaination

ps. edit to show what I've tried first i make array of hash then do something like this I thought it wouldn't be confused this much

result = [] arr.each do |b| if result.any? {|r| r[:name] == b[:name]} if result.first[:children].any? {|r| r[:name] == b[:children].first[:name]} if result.first[:children].any?{|c| c[:children].any? {|r| r[:name] == b[:children].first[:children].first[:name] && c[:name] == b[:children].first[:name] }} if result.first[:children].any? {|r| r[:children].any? {|c| c[:children].any?{|k| k[:name] == b[:children].first[:children].first[:children].first[:name] && (c[:name] == b[:children].first[:children].first)}}} else result.first[:children].any?{|c| c[:children].any? {|r| r[:name] == b[:children].first[:children].first[:name] ; r[:children] << b[:children].first[:children].first[:children].first}} end #fourth else result.first[:children].any? {|r| r[:name] == b[:children].first[:name]; r[:children] << b[:children].first[:children].first} end else result.any? {|r| r[:name] == b[:name] ; r[:children] << b[:children].first} end else result << b end end

最满意答案

你可以这样做递归:

def map_objects(array, level = 1) new_obj = [] array.group_by(&:shift).each do |key, val| new_obj << {:name=>key, :level=>level, :children=>map_objects(val, level + 1)} if key end new_obj end

对于你的数组,它会像这样返回:

# [ # {:name => "England", :level => 1, :children => [ # {:name => "London", :level => 2, :children => [ # {:name => "University College London ", :level => 3, :children => [ # {:name => "Faculty of Law", :level => 4, :children => [] # }, # {:name => "Faculty of Engineering", :level => 4, :children => [] # }] # }, # {:name => "Imperial College London", :level => 3, :children => [ # {:name => "Faculty of Medicine", :level => 4, :children => [] # }] # }] # }, # {:name => "Manchester", :level => 2, :children => [ # {:name => "University of Manchester", :level => 3, :children => [ # {:name => "Faculty of Engineering", :level => 4, :children => [] # }] # }] # }] # } # ]

You could do this recursive like this:

def map_objects(array, level = 1) new_obj = [] array.group_by(&:shift).each do |key, val| new_obj << {:name=>key, :level=>level, :children=>map_objects(val, level + 1)} if key end new_obj end

For your array it will return like this:

# [ # {:name => "England", :level => 1, :children => [ # {:name => "London", :level => 2, :children => [ # {:name => "University College London ", :level => 3, :children => [ # {:name => "Faculty of Law", :level => 4, :children => [] # }, # {:name => "Faculty of Engineering", :level => 4, :children => [] # }] # }, # {:name => "Imperial College London", :level => 3, :children => [ # {:name => "Faculty of Medicine", :level => 4, :children => [] # }] # }] # }, # {:name => "Manchester", :level => 2, :children => [ # {:name => "University of Manchester", :level => 3, :children => [ # {:name => "Faculty of Engineering", :level => 4, :children => [] # }] # }] # }] # } # ]

更多推荐

本文发布于:2023-08-07 21:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466037.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   数组   ruby   creating   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!