Is it possible to employ the 'index' parameter within a 'v-for' loop that is nested inside a 'method'?

Is it possible to use the index on a v-for directive at the same level as the directive itself in order to manipulate the state being displayed?

<template>
  <div>
    <div v-for="share in sharesPurchased()" :key="share">
      <div>
        <h4>This is some content</h4>
        <p style="border: solid 1px">{{share.count}}</p>
      </div>
    </div>
  </div>
</template>

<script>

  export default {
    data(){
      return {
        shares: [
          {id: 'BMW', count: 1},
          {id: 'Ford', count: 0},
          {id:'Apple', count: 10}
        ]
      }
    },
    methods: {
      sharesPurchased() {
// Looking to use the index within v-for to control content display
      }
    }
  }
</script>

I am trying to filter the content displayed in this loop to only show items with a count > 0, for example: shares[i].count > 0

If successful, only the value 1 should be displayed in the

<p style="border: solid 1px">{{share.count}}</p>
, as only this.shares[0].count is greater than 0.

Answer №1

Update: After considering your specific requirements, it appears that you may be experiencing an X-Y problem. It would be more appropriate to utilize computed properties in this scenario:

computed: {
    filteredSharesByCount: function() {
        return this.shares.filter(share => share.count > 0);
    }
}

You can then use this computed property in your template:


Previous answer:

You can access the index as a second optional argument in the v-for binding:

v-for also supports an optional second argument for the index of the current item.

For example:

<div v-for="(share, idx) in sharesPurchased(shares)" :key="share">

Within the loop, you can use idx to access the index as needed.

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

Determine ng-checked according to an array

I am working with a select dropdown that creates checkboxes using ng-repeat based on the selected value. The goal is to have all values in the dropdown selected, corresponding checkboxes checked, and store them in an array. Each time a checkbox is changed ...

Dealing with problematic hover behaviors in Cypress: A guide

I encountered an issue with Cypress hover functionality while trying to access a sub menu that appears after hovering over a main menu item. The error message I received was This element is not visible because it has CSS property: position: fixed and it&ap ...

Code running successfully in Chrome's console, but encountering issues when executed on the webpage

Having some trouble assigning a function to a button in JavaScript. This is the button I'm working with, and my main goal right now is to make it responsive. <button id="artbtn" class="artbtn btn">Art</button> I experimented with this in ...

Invalid Syntax: The token '21' is found unexpectedly at column 12 in the expression [2013-08-28 21:10:14] beginning at [21:10:14]

I'm in the process of creating a straightforward directive for a date countdown. However, I've hit a roadblock with this particular error: Syntax Error: Token '21' is an unexpected token at column 12 of the expression [2013-08-28 21:10 ...

Dealing with a situation where different functions need to be called based on a condition while using unique button names. This is

<button type="button" class="btn btn-primary ms-4" (click)="update()">Save</button> <button type="button" class="btn btn-primary ms-4" (click)="create()">Add</button> B ...

Tips on creating type definitions for CSS modules in Parcel?

As someone who is brand new to Parcel, I have a question that may seem naive. In my project, I am using typescript, react, less, and parcel. I am encountering an error with typescript stating 'Cannot find module 'xxx' or its corresponding t ...

Preserve setTimeout() Functionality Across Page Refreshes and Navigations

I am trying to display an alert message after a 15-minute delay, but the functionality is disrupted when the page refreshes or if I navigate to a different page. This all takes place on a single web page. When a specific button is clicked, it should trig ...

Limit the 'contenteditable' attribute in table data to accept only integers

I have a question regarding editing table data row. Is there a way to restrict it to only integers? Thank you for your assistance! <td contenteditable="true" class="product_rate"></td> ...

Access your login page with a nodejs mongoDB connection

After successfully sending username, password, and email through postman, the information gets added to the MongoDB schema. However, on the login page in React, it doesn't seem to work correctly. Any assistance would be greatly appreciated. Thank you. ...

What is the best way to change a javascript string into HTML so that it can be shown in a form?

I am struggling to find a solution to this issue. I am fairly new to jQuery and JavaScript, so please forgive me if my question seems basic. I am trying to call a cfc (ColdFusion) using jQuery and retrieve HTML data. However, when I receive the data, it ...

Personalize the year heading for v-date-picker

I am utilizing a date picker with the Japanese locale <v-date-picker locale="ja-jp" ></v-date-picker> This feature allows for kanji characters to be displayed for week/month/year. However, I am curious ...

What could be causing the NextJS query string to be undefined upon reloading the page?

I am currently working on a NextJS application where I have created a search results page that relies on a query parameter being passed. To grab the query string, I am using the next/router package. The issue I am facing is that after the initial load of t ...

Optimizing Wordpress by Efficiently Enqueueing Javascript

As a beginner with a WordPress website, I am aware that in order to execute scripts on a WordPress page, they need to be enqueued in the functions.php file. However, I'm unsure about the correct process for this. The specific JavaScript file I want t ...

Python responding to an Ajax request using jQuery

Currently, I am experimenting with integrating a pre-built inline editor from the following source: https://github.com/wbotelhos/inplace Regrettably, the available support documentation is lacking and my expertise in Javascript, jQuery, or Ajax is limited ...

Ensure that reactivity is applied only to nested properties

My data object consists of properties unrelated to vue/the UI and data that represents the state. I only want the state to be reactive, but I still need the complete object in the component. It's important that vue doesn't modify the other proper ...

Error: Attempting to access the 'state' property of an undefined variable within the bootstrap framework

While attempting to update the state value, I encountered an error stating TypeError: Cannot read property 'state' of undefined. This error occurs when I enter something in a field and then click submit. As a newcomer to React, I realize this may ...

Is there any functionality provided by vue.js filters that cannot be achieved with nested methods?

Is there a significant advantage to using vue.js filters over nested methods? As a newcomer to vue.js, it seems like additional syntax with little tangible benefit. For example, instead of utilizing the "capitalize" function in filters as shown below: {{ ...

The jQuery click event is failing on the second attempt

My PHP code dynamically generates a list, and I want to be able to delete rows by clicking on them. The code works fine the first time, but not the second time. HTML <li> <div class="pers-left-container"> <img src="<?php ech ...

The React data editor Dialog closes when the drop-down is clicked without triggering any onChange events

Utilizing the react-datasheet component, I have implemented a table to display a matrix/grid of data. To enhance user experience, I customized the dataEditor to launch a custom dialog where users can only choose from preselected values in a material-ui dro ...

The HTTP response object in Express does not provide any data in its return

While working on developing a server using express js, I encountered an issue. When I send a get request to my endpoint from another server, the response is received successfully; however, it does not contain the data that was sent by the server after res. ...