Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:26:23+00:00 2026-05-31T11:26:23+00:00

I am trying to remove elements from an array if a value in that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:26:24+00:00Added an answer on May 31, 2026 at 11:26 am

    @steenslag gave me the idea that offered up the solution for my problem.

    resources.delete_if do |resource| 
      (resource.tags.map(&:name) & no_tags).present? 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove all HTML elements from a String. Unfortunately, I cannot
Let's say I am trying to remove elements from array a = [1,1,1,2,2,3] .
I am trying to remove duplicate elements from an excel array. I imagine I'm
I have an nested array and want to remove the entries that matches the
I am trying to remove elements from a Dictionary<string, List<string>> in C# when the
I have a 2-d array xx=[[a,1],[b,2],[c,3]] Now I'm trying to remove duplicate entries from
I am trying to remove an element from a Javascript associtive array using the
I am trying to remove all script elements from a HTML page. But for
I am trying to add and remove elements from a list mapped as .HasMany(),
I'm trying to dynamically delete elements from an array in bash based on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.