I am trying to remove elements from an array if a value in that array matches a given string.
I have an array that is has a tags array. I am comparing the names of the tags to see if they match what the user wishes to exclude from their search. If it does I would like to either remove that element from the main array, or else possibly add the element that does not match.
Here is what I have so far:
results = Array.new
test = 0
no_tags.each do |no_tag| #an array of tags whose resources are not to be included
resources.each do |r|
add_to_array = false
r.tags.each do |t|
if t.name.eql? no_tag
test += 1
add_to_array = false
else
add_to_array = true
end
end
if add_to_array
results << r
end
end
end
The test variable is just a variable for debugging the number of matching occurrences which happens to be 141 out of 763 resources. However when I do results.count after this block is run I only get 732 when I should be getting 622.
To clarify I need to either delete the element of the resource array if the tags array contains a match, or the other option would be to include the resource array element into a new array if a match is not found.
This will be returned to the browser as JSON and I need to exclude resources whose tags match the values of the no_tags array.
@steenslag gave me the idea that offered up the solution for my problem.