A solution for accessing computed properties within a v-for loop

Currently, I am facing a challenge with the code provided below. It seems that computed properties do not support parameters. Do you happen to have any suggestions on how to overcome this issue? I am considering using watchers on functions but I am also exploring if there is a simpler solution available.

var app = new Vue({
    el: '#app',
    data() {
      return {
        sessions: {
          "156": {
            tickets: {
              "01": {
                available: true,
              },
              "02": {
                available: false,
              },
            }
          },
        },
        tickets: {
          "01": {
            attr: "somestring",
          },
          "02": {
            attr: "someotherstring",
          },
        },
      },
    };
  },
  computed: {
    sessionTickets(session) {
        let _this = this;
        let sessionTickets = {};
        $.each(_this.session.tickets, function(ticketId, sessionTicket) {
            if(sessionTicket.available) {
                sessionTickets[ticketId] = _this.tickets[ticketId];
            }
        });
        return sessionTickets;
    },
  },
});

<div v-for="session in sessions">
  <div v-for="sessionTicket in sessionTickets(session)">
    {{ sessionTicket.attr }}
  </div>
</div>

Answer №1

Big shoutout to "WallOp" for helping me see that my computed property was stuck in a sessions loop, leading me to convert it into a class method that can now be easily refreshed upon session refresh!

Answer №2

One solution could be utilizing computed properties in Vue. In this case, you can filter tickets of sessions as shown below:

var app = new Vue({
  el: '#app',
  data() {
    return {
      sessions: {
        "156": {
          tickets: {
            "01": {
              available: true,
            },
            "02": {
              available: false,
            },
          }
        },
      },
      tickets: {
        "01": {
          attr: "somestring",
        },
        "02": {
          attr: "someotherstring",
        },
      },
    },
  };
  computed: {
    filteredSessions() {
      return this.sessions.map( session => {
        let tickets = {};
        for(key in session.tickets) {
          if(session.tickets[key].available && this.tickets.hasOwnProperty(key)) {
            tickets[key] = this.tickets[key];
          }
        }
        session.tickets = tickets;

        return session;
      });
    },
  },
});

<div v-for="session in filteredSessions">
  <div v-for="ticket in session.tickets">
    {{ ticket.attr }}
  </div>
</div>

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

Unable to locate an element on the webpage due to a JavaScript-based error, which then becomes hidden after a few seconds. (Registration form)

While completing a registration form, I encounter a hidden message after clicking on the register button. Struggling to locate this elusive element has been an ongoing challenge for me. Unfortunately, my attempts to find the element have been unsuccessful ...

Retrieve elements from an array based on the value of an object

I have a list of items that resembles the following structure: var entries = [ { sys: {id:"1"}, fields: "article1" }, { sys: {id:"2"}, fields: "place1" }, { sys: {id:"3"}, fields: "offer2" }, { sys: {id:"1"}, fields: "article2" }, { sys: {id:"1" ...

What is the best way to pass the setState value to the useEffect hook?

After watching a Youtube video, I took on the challenge of creating my own recipe app using React.js as a beginner. I have been troubleshooting for the past 2 days and seem to have hit a roadblock. The issue lies in passing the value of my state to the use ...

Traverse Through Collection of Vue Elements

I am working on creating an array of Vue Components based on a configuration file containing various UI sections to display; const config = [ 'summarySection', 'scoreSection', 'educationSection' ] I am attempting to ma ...

Can a library be developed that works with both Java and JavaScript/TypeScript?

I specialize in Angular development. Our front- and backend both contain specialized calculation methods that work like magic. Although the classes are the same, any bugs found in the calculations have to be fixed separately in two different projects. Is ...

Generate a sequence of years without relying on the range function

Is there a different approach to generating this array without relying on the range function? Below is an illustration of what I want, but without utilizing the range method. const years = myCustomArrayGeneration(1990, getYear(new Date()) + 1, 1); ...

Showcasing ranges from various input types on a single page

I have a challenge in displaying the values of multiple sliders on my webpage. Here's the code snippet I've been working on: var i = 0; var st = 'slider'; var ot = 'output'; var s = ''; var o = ''; for ...

Ways to host static content in ExpressJS for specific directories and exclude others

I need to configure my ExpressJS server to serve static files from specific paths only, excluding others. For example, I want to serve static files from all paths except /files where I only need to manipulate a file on the server. Currently, my code looks ...

Dealing with errors in a sequelize ORM query: Tips and tricks

Currently, I am implementing Sequelize ORM in my Node/Express project using Typescript. Within the database, there is a 'users' table with a unique column for 'email'. As part of my development process, I am creating a signIn API and ...

Page reloads are disabled when Chrome devtools debugger is paused in a React app

Currently, I am in the process of troubleshooting a React application that was created using create-react-app. Whenever I attempt to reload the page while paused on a breakpoint, it results in the page stalling. The screen goes blank and is unresponsive t ...

What is the process for cancelling an interval when it is disabled in my configuration file?

To automate a bot, I want it to stop running an interval if the configuration file specifies "off" and continue running if it says "on". I attempted this: Using discord.js: config.Interval = setInterval(() => { WallCheck.send(WallCheckemb ...

What could be causing this empty Ajax ResponseText?

$("b_xml").onclick=function(){ new Ajax.Request("books.php", { method:"GET", parameters: {category:getCheckedRadio(document.getElementsByName("category"))}, onSuccess: showBooks_JSON, onFailure: ajaxF ...

Creating a service in AngularJS 1.8 with ES6 modules that acts as a bridge to a class based API interface

As I continue to enhance a codebase that originally consisted of a mix of different versions of AngularJs and some unstructured code utilizing various versions of a software API, I encounter an interesting quirk. It appears that this API is accessible thro ...

What is the best way to configure Jenkins to exclude or include specific component.spec.ts files from being executed during the build

Currently, I am facing an issue while attempting to include my spec.ts files in sonarqube for code coverage analysis. However, my Jenkins build is failing due to specific spec.ts files. Is there a way to exclude these particular spec.ts files and include ...

Ensure all fields are valid prior to performing an update

I'm currently faced with the challenge of validating my form data before executing an AJAX update. Essentially, what I want to achieve is to validate the input data in the form before triggering the ajax update function. I'm unsure about where to ...

The functionality of Vue.js routes is disrupted when employing the router-link component

I recently worked on a project using Vue.js. In the navbar, I added two menu options - Home and About, which are supposed to direct users to specific pages when clicked. Everything runs without any errors, but for some reason, the router doesn't navig ...

Verifying input for Numeric TextField

The text field being used is: <TextField variant="outlined" margin="normal" id="freeSeats" name="freeSeats" helperText={touched.freeSeats ? errors.freeSeats : ''} error={touched.freeSeats && Boolean(errors.fre ...

encountering difficulties with installing dependencies using yarn or npm

After cloning a repository, I attempted to install the dependencies using npm install or yarn but encountered the following errors: For Yarn: https://gyazo.com/2fdf52c4956df2e565cc0b1cedf24628 For npm install: https://gyazo.com/a1d197e9ead89dbe4a7d3c5b8f ...

Adjust Div Width using a button press

I am attempting to create a functionality where an iFrame will resize in width upon clicking a button. My goal is to have multiple buttons representing breakpoints such as 1024, 680, 480, 380 pixels where the iFrame will adjust its size accordingly. Unfort ...

Finding the absolute root path of a npm package in Node.js

Objective I am on a quest to find a reliable method to ascertain the absolute root path of an npm package that has been installed in Node.js. Challenge Although I am aware of require.resolve, it only provides the entry point (path to the main module) an ...