1

I have a list of strings in an array like so...

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

Then I have an array of hashes like so...

[
  {"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" }
]

What I want to do is remove any item from the array of hashes where attributes['name'] matches any of the names in the playlist_track_names array.

How would I do that?

4

2 Answers 2

2

It looks like your hash_list is missing some closing brackets. I've added them below. Try running this in irb:

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

hash_list = [
  {"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" } }
]

hash_list.delete_if { |i| playlist_track_names.include? i["attributes"]["name"] }
puts hash_list
Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array#delete_if to delete any entries which match a block. In the block use Array#include? to check if the track name is in the list.

tracks.delete_if { |track|
  playlist_track_names.include? track["attributes"]["name"]
}

Note that because playlist_track_names.include? has to search through playlist_track_names one by one this will get slower as playlist_track_names gets larger. You can avoid this by using a Set.

require 'set'

playlist_track_names = ["I Might", "Me & You", ...].to_set

A Set is like a Hash with just keys, no values. They're an unordered collection of unique values which are very fast to lookup. playlist_track_names.include? on a Set will perform the same no matter how large playlist_track_names gets.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.