Ways to retrieve information obtained through a function for each object in v-for loop

When looping through a list using v-for, I am calling a method for each item. The method returns the necessary object, but I need to be able to access that information in other parts of my .card. For example, I want the {{name}} to display the sponsor's name returned by the method. Typically, I would store the data in Vue data and access it via the item, but with over 200 items this doesn't seem like the best approach (unless I'm missing something in my logic).

Here is how my VueJS setup looks:

HTML:

<div v-for="deal in deals">
  <div class="card">
    <h3>{{deal.name}}</h3>
    <p>{{ getSponsor(key) }}</p>
    <p>{{sponsor.name}}</p>
  </div>
</div>

JS:

firebase () {
  return {
    deals: db.ref('deals'),
    businesses: db.ref('businesses')
  }
},
methods: {
  getDealSponsor (key) {
    db.ref('businesses').child(key).on('value', snap => {
      return snap.val()
    })
  }
}

JSON:

deals
  -SomeDeal(Firebase Key)
    name: "Some Deal"
    provider: -SomeBusiness (Firebase Key)
businesses
  -SomeBusiness (Firebase Key)
    name: "Some Business Name"

Answer №1

Although it may seem impossible to create a "temporary variable" within a loop, you can take advantage of the fact that v-for creates an alias for the items being iterated over:

<div v-for="item in items">
  <div class="box">
    <h3>{{item.title}}</h3>
    <template v-for="detail in [getDetail(item.key)]">
      <p>{{ detail }}</p>
      <p>{{detail.name}}</p>
    </template>
  </div>
</div>

If key is merely a property passed down to the component, you can create a computed property based on it:

details() {
  return getDetail(key);
}

You can then utilize this computed property in a similar way as demonstrated with the alias above.

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

Using CSS and JavaScript to hide a div element

In my one-page website, I have a fixed side navigation bar within a div that is initially hidden using the display:none property. Is there a way to make this side nav appear when the user scrolls to a specific section of the page, like when reaching the # ...

Retrieving the selected option from a dropdown list in VueJS

I have been struggling to store the selected value from a dropdown list as a data object in Vue. Despite my attempts, I am unable to successfully save the value in the Vue data object. I have experimented with using onchange and v-model.lazy, but I am unsu ...

Selenium and AngularJS patiently wait before carrying out specific actions

I have been using selenium to test an AngularJS application and I am encountering an issue where I am unable to perform any actions on the page until it is fully loaded. Although I have considered using Thread.sleep(), I am aware that this is not the most ...

Enhancing late-bound properties in vue.js with custom getters and setters

This particular issue pertains to the following query: map two 1D arrays into a 2D array and then fill with known values. Essentially, I am working with three sets of data: Colours, Sizes, and Products. These datasets are loaded into the Vue main component ...

Heroku encounters an issue when attempting to launch the application using the command"npm start."

Every time I deploy my application to Heroku, the build process goes smoothly. However, as soon as it attempts to run the start script using npm start, it gets stuck and eventually crashes. I've tried various troubleshooting steps without success, and ...

Having difficulty passing the new state value using props in ReactJS

I am facing an issue with sending state values from parent to child components. Initially, the state value is empty but after making an API call, the state value gets updated. However, the props in my child component are still stuck with the initial empty ...

Rails 7: It appears that the browser is disregarding all importmap JavaScript packages

Last week I worked through two different tutorials, successfully achieving most of the tasks. However, three crucial issues remain unresolved. Upon closer inspection, it became evident that all three problems revolve around the browser seemingly disregardi ...

What is the proper way to input information into fields during an update?

Let's talk about a scenario: I have a form created using Material UI components (TextFields) that I want to use for both creating and updating products. The product creation part of the form is working well, except when no image is added. For updati ...

Disabling Scroll for a HTML Input Tag

My input field has a long value and is set to readonly. Although I have applied overflow-x: hidden; to it, scrolling is still possible on mobile devices. I want to disable this scroll behavior. Can anyone provide a solution? Thank you for your assistance. ...

Bootstrap Dropdown Functionality Malfunctioning

Here is a simple piece of HTML code that I have created: <!doctype html> <html> <head> <meta charset="utf-8"> <title>.:Home Page:. The Indian Sentinel</title> <link rel="stylesheet" href=" ...

Rapidly refreshing user interface in real-time based on current status

I'm struggling to create a button that will smoothly open a menu. Initially, I thought setting the state on button click would work fine but now I realize that the first click does not have any effect on the state or class. It's only the second a ...

Extract the URL parameter and eliminate all characters following the final forward slash

Here is the URL of my website: example http://mypage.com/en/?site=main. Following this is a combination of JavaScript and PHP code that parses the data on the site. I am now looking for a piece of code that can dynamically change the URL displayed in the ...

"Learn the process of setting a variable in ng-model within an input field based on a specific condition

I need to dynamically assign an ng-model variable based on a condition. For instance: <input type="text" ng-model="item.model[multilang]" > The $scope.multilang variable can be set to either "ENG", "JP" (languages) or false. So, when multilang = "E ...

The document's event listener for the "click" event on the element with the ID of "myId" is unable to properly update the owl carousel

I have been experimenting with the Carousel example and modifying the function to enable loading on click. Here is my updated code snippet: $(document).on("click","#myId",function(){ $("#owl-demo").owlCarousel({ autoPlay : 3000, //Set AutoPlay ...

Determine whether a child element is missing a certain class

I am currently investigating whether a child element lacks a specific class. The elements in question are: <g id="note1"> <path id="Barline1" d="M55.837,19.278h0.249c0.11,0,0.199,0.089,0.199,0.199V40.56c0,0.11-0.089,0.199-0.199,0.199h-0.249c ...

javascript code for subtracting values in arrays

I am facing a scenario where I have 3 arrays of the same length. My requirement is to automatically subtract the values in the first two arrays without any user input. If the result of subtraction is negative, I need to identify the index of that array num ...

When a Vue.js Directive is inserted or bound, it actually takes precedence over the click event

Imagine having multiple dropdowns or elements on the page that all utilize a directive called "closable". This directive triggers an expression if the element clicked is outside of the element using the directive. The intended behavior is that when clicki ...

Disabling prefetch in next.config.js: A comprehensive guide on eliminating data prefetching in Next.js

Typically, in many cases, the disabling of prefetching is achieved by specifically setting a link to not preload. An example of this can be seen below: <Link href="/about" prefetch={false}> <a>About us</a> </Link> My go ...

Storing external API requests in create-react-app's service worker for faster retrieval

I'm in the process of transforming a React web application into a PWA (Progressive Web App). I've made the necessary change in the index.js file - serviceWorker.register();. Everything is functioning properly as I can access the home page and as ...

Encountering errors when importing a JS file into a Vue project

I recently attempted to utilize the plugin https://www.npmjs.com/package/mobius1-selectr After running npm install mobius1-selectr I proceeded to import it into my main.js import 'mobius1-selectr/dist/selectr.min.css'; import 'mobius1-sel ...