【发布时间】:2018-08-29 21:39:19
【问题描述】:
我有一个像这样的数组中的字符串列表......
playlist_track_names = ["I Might", "Me & You", "Day 1", "I Got You (feat. Nana Rogues)", "Feels So Good (feat. Anna of the North)", "306", "Location Unknown (feat. Georgia)", "Crying Over You (feat. BEKA)", "Shrink", "I Just Wanna Go Back", "Sometimes", "Forget Me Not"]
然后我有一个这样的哈希数组......
[
{"id"=>"1426036284", "type"=>"songs", "attributes"=>{"name"=>"I Might", "albumName"=>"Love Me / Love Me Not" },
{"id"=>"1426036285", "type"=>"songs", "attributes"=>{"name"=>"Feels So Good (feat. Anna of the North)", "albumName"=>"Love Me / Love Me Not" },
{"id"=>"1426036286", "type"=>"songs", "attributes"=>{"name"=>"Forget Me Not", "albumName"=>"Love Me / Love Me Not" },
{"id"=>"1426036287", "type"=>"songs", "attributes"=>{"name"=>"Some Other Name", "albumName"=>"Love Me / Love Me Not" }
]
我想要做的是从哈希数组中删除 attributes['name'] 与 playlist_track_names 数组中的任何名称匹配的任何项目。
我该怎么做?
【问题讨论】:
-
顺便说一句,Stack Overflow 上有数以万计的关于“我如何处理这个字符串到字符串的哈希数组”或类似问题的问题,答案总是一样的: Ruby 是一种面向对象的语言,而不是面向哈希数组的从字符串到字符串的语言。重新设计你的数据模型,让你一开始就没有那些复杂的结构,然后你就不需要处理它们了。
-
@JörgWMittag 同意。这个疯狂的数据模型来自我无法控制的第三方 API。我很想不必处理这个问题。 :)
-
那就别处理了。将其包装到您自己的数据模型中。