解析嵌套 Ruby 数组/YAML 的最快方法
如果它不对称,从该结构中提取第三级元素的最简洁方法是什么:
yaml = [
{"Collection"=>[
{"Jeans"=>[
{"Trends"=>[
{"Building on basics"=>"Jeans"},
{"Unexpected pairings"=>"Jeans"},
{"Retro chic"=>"Jeans"} # extract me
]},
{"Styles"=>[
{"Straight Leg"=>"Straight Leg"},
{"Bootcut"=>"Bootcut"},
{"Trouser"=>"Trouser"},
{"Slim Leg"=>"Slim Leg"}
]},
{"Wash"=>[
{"Blues"=>"Blues"},
{"Black/Grey"=>"BlackGrey"},
{"Whites"=>"Summer Whites"},
{"Colors"=>"Colors"},
{"Resin"=>"Resin"}
]},
{"Details"=>[
{"Embroidered"=>"Embroidered"},
{"Tuxedo"=>"Tuxedo"},
{"Jeweled"=>"Jeweled"}
]}
]},
{"Bodyshapers"=>[
{"All"=>[
{"All"=>"BodyShapers"}
]}
]}
]},
{"Lift Tuck"=>nil},
{"Find Us" =>[
"By City",
"International Websites",
"Online Retailers"
]},
{"Your Stories"=>nil},
{"The Skinny"=>[
"Trends",
"Behind the Scenes",
"VIP Events",
"In the News",
"Win a Pair"
]}
]
What's the cleanest way to extract third level
elements from this structure provided that it's not symmetrical:
yaml = [
{"Collection"=>[
{"Jeans"=>[
{"Trends"=>[
{"Building on basics"=>"Jeans"},
{"Unexpected pairings"=>"Jeans"},
{"Retro chic"=>"Jeans"} # extract me
]},
{"Styles"=>[
{"Straight Leg"=>"Straight Leg"},
{"Bootcut"=>"Bootcut"},
{"Trouser"=>"Trouser"},
{"Slim Leg"=>"Slim Leg"}
]},
{"Wash"=>[
{"Blues"=>"Blues"},
{"Black/Grey"=>"BlackGrey"},
{"Whites"=>"Summer Whites"},
{"Colors"=>"Colors"},
{"Resin"=>"Resin"}
]},
{"Details"=>[
{"Embroidered"=>"Embroidered"},
{"Tuxedo"=>"Tuxedo"},
{"Jeweled"=>"Jeweled"}
]}
]},
{"Bodyshapers"=>[
{"All"=>[
{"All"=>"BodyShapers"}
]}
]}
]},
{"Lift Tuck"=>nil},
{"Find Us" =>[
"By City",
"International Websites",
"Online Retailers"
]},
{"Your Stories"=>nil},
{"The Skinny"=>[
"Trends",
"Behind the Scenes",
"VIP Events",
"In the News",
"Win a Pair"
]}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道最简单的方法,但这样的方法会起作用:
I don't know about the easiest way, but something like this would work: