rails neglecting to include in the array

Could someone help me with this block of code I have:

doc.xpath("//script[@type='text/javascript']/text()").each do |text|
       if text.content =~ /more_options_on_polling/
         price1 = text.to_s.scan(/\"(formatted_total_price)\":\"(.+?)\"/).uniq
         description = text.to_s.scan(/\"(ticket_desc)\":\"(.+?)\"/).uniq
         price = price1 + description
         testing = price1.map{|a| { a[0] => a[1] } }
         desc = description.map{|a| { a[0] => a[1] } }
         respond_to do |format|
           format.json  { render :json => {:testing => testing,
                                           :desc => desc }}
         end
       end

When I check the 'desc', It shows the following info:

"desc": [
    {
      "ticket_desc": "Later Owl Ticket"
    },
    {
      "ticket_desc": "Later Owl Ticket+Collector Ticket &#64 extra £4.95 per ticket"
    },
    {
      "ticket_desc": "Later Owl + Chance For VIP Upgrade"
    },
    {
      "ticket_desc": "VIP Ticket"
    },
    {
      "ticket_desc": "VIP Ticket + Collector Ticket &#64 extra £4.95 per ticket"
    },
    {
      "ticket_desc": "Skydeck Package"
    },
    {
      "ticket_desc": "5 Person Skydeck Table"
    },
    {
      "ticket_desc": "7 Person Skydeck Table"
    },
    {
      "ticket_desc": "10 Person Skydeck Table"
    }
  ]

What I want is to exclude any ticket descriptions with a + symbol. So the desired output would be:

"desc": [
    {
      "ticket_desc": "Later Owl Ticket"
    },
    {
      "ticket_desc": "VIP Ticket"
    },
    {
      "ticket_desc": "Skydeck Package"
    },
    {
      "ticket_desc": "5 Person Skydeck Table"
    },
    {
      "ticket_desc": "7 Person Skydeck Table"
    },
    {
      "ticket_desc": "10 Person Skydeck Table"
    }
  ]

I'm working on this using Rails, and I am rendering it in the view using JavaScript/Ajax. If you have any suggestions on how to handle this, please share them :)

Here's the Ajax snippet being used:

for (var i = 0; i < json.testing.length; i++) {
    var section = json.testing[i].formatted_total_price;
    var desc = json.desc[i].ticket_desc;

Answer №1

One way to filter the desc hash is by using the select method. For instance, you can insert the following code after the line starting with desc =:

desc.select! { |item| !item['description'].include?("special") }

If you wish to preserve the original desc, you can use the non-destructive select and store the filtered data in a new variable:

desc_without_special = desc.select { |item| !item['description'].include?("special") }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble inserting a Button element into a li parent element

I'm attempting to insert a button inside an li element using the .appendChild method, but for some reason, it's not working. I also tried changing the parent's inner HTML. let inputElement = document.querySelector('#input'); let ...

Tips for displaying an element in a fresh container using jQuery

I am looking to add draggable functionality to the objects in columns on my page. Below is the script I have implemented: <script> $(document).ready(function () { $('.application').draggable({ helper: 'clone' }); ...

Is there a way to stop "window.location.replace()" from being replaced or overridden?

Is there a method to safeguard against alterations or overrides of window.location.replace()? For instance, attempting to change it like this: window.location.replace = function(){ return "Hi" }. Initially, I experimented with using Object.free ...

What sets index.js apart from page.js in next.js?

https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts While reading through the next.js documentation, I came across an interesting point. The documentation mentions that index.js serves as the root of the directory. This mean ...

I've been attempting to relocate a CSS element at my command using a button, but my previous attempts using offset and onclick were unsuccessful

I am trying to dynamically move CSS items based on user input or button clicks. Specifically, I want to increment the position of elements by a specified number of pixels or a percentage of pixels. Currently, I am able to set the starting positions (top a ...

Adjust the vertical alignment of an HTML element

I've been trying to make an image move using the variable counter, but for some reason, it's not working. If I set document.getElementById("movee").style.top directly to a number, it works fine. Combining it with the counter variable should theor ...

What is the best way to create an image carousel that continuously loops instead of resetting?

Currently tackling an issue with my image carousel project. The automatic function of the slider is not functioning as desired. Instead of looping back to the first image when it reaches the last one, it moves all the way back to the beginning, displaying ...

Enter information into the TextArea and jQuery dialog

I require assistance in populating a textarea with data obtained from querying a database. I have a jQuery dialog that contains another dialog, inside of which is a textarea. Here is some pseudocode: <MODAL> <modalB> <TextArea>s ...

Generating a dynamic clickable div using Angular 6

How can I create a user-friendly interface that displays an image with clickable divs around detected faces, allowing users to view specific attributes for each face? I'm looking for a solution where I can have divs or buttons around each face that t ...

Navigating between two table components in React JS

I am a beginner in the world of React and I am struggling with switching between these two tables. Despite consulting the documentation for inline conditional statements, I still couldn't figure it out. My goal is to have the tables switch after click ...

Unraveling the mystery: Retrieving data from various child components within a Vue parent component

Currently, I am in the process of constructing a view that includes a primary component called ContentComponent. This component acts as a container for a series of sub-components, each representing a form module. The list of forms include: EvaluationForm ...

Identifying repetitive elements within an array of objects using JavaScript/Node.js

In my code, I have an array named offers containing objects structured like this: { sellItem: { id: _, quantity: _ }, buyItem: { id: _, quantity: _ }, volume: _ } My goal is to identify duplicates w ...

Displaying Images with React Components Using JSON Paths

I'm encountering an issue with my React component that is fetching data from a local JSON file. The data includes links to images, but for some reason, the images are not loading despite the paths being correct. Previously, I used 'require' ...

What is the best way to integrate an admin template into a website without relying on popular CMS platforms like WordPress?

Do I need to use a WordPress system in the backend in order to utilize an admin template? I've noticed that there are several sleek web admin templates available in Bootstrap. I'm interested in using one of them but unsure how to do so without re ...

"We are unable to set a value for a global array unless we utilize the .push() method

It's puzzling that I can't populate a global array without using the .push() method. (function() { var globalEmail = []; var testUpdate = function() { var arr = [1, 2, 3]; if (globalEmail.length > 1) { gl ...

I'm having trouble with my array in C# continually getting filled with empty data

Recently started learning C# and I am facing a challenge. We are required to create an array that can store up to 100 scores, which must also accommodate smaller inputs from a text file containing numbers between 0-100. The problem arises when I add a te ...

Set a class for the active menu item using jQuery based on the current window location

I have an HTML snippet here with a class called js-nav and a custom attribute named data-id. This data is crucial for determining the current sliding menu. <a class="nav-link js-nav" data-id="about" href="/#about">About</a> The approach I too ...

Searching for a specific bus stop within an array based on a designated route

There's an issue at hand. We have a route with several bus stops. We need to determine the next stop on this route. The "_stopNum": "1" indicates that the stop is the first one on the route. Take a look at a stop example: { "route": [ { ...

Using jQuery's .load() method to load a PHP file can result in an exponential increase in XHR finished loading time with each subsequent load

There seems to be an issue with my code using .load() to load a PHP page into a div whenever the navbar or link is clicked. After multiple clicks, I noticed that the "XHR finished loading" increases exponentially and it appears that the same PHP file is be ...

Display handpicked ionic list view

I have put together a demonstration using this (demo) to showcase the issue I am facing. Within this demo, there is a page with a list, a button located in the header that checks the attribute of an <ion-checkbox>, and a checkbox next to the list v ...