2

Please refer below code. Here I am using v-for to iterate over the object.

In this I am trying to access index parameter but I am getting it blank.

<a href="#" class="list-group-item" v-for="(event, key, index) in events">
    index is {{index}}
</a>

I also tried below way but got the same result:

<a href="#" class="list-group-item" v-for="event in events">
    index is {{$index}}
</a>

This is my object:

var events = [{
            name: 'event1',
            description: 'description 1',
        },
        {
            name: 'event2',
            description: 'description 2',
        }
    ];

Can anybody please help me out with this. How can I access index property here.

Thanks in advance.

2
  • Of what type is your events object? Commented May 31, 2017 at 7:37
  • events is type of array.. In which I am storing objects...Please refer the updated question Commented May 31, 2017 at 7:44

2 Answers 2

1

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

And there is no third argument. So you should write like this

<a href="#" class="list-group-item" v-for="(event, index) in events">
    index is {{index}}
</a>
Sign up to request clarification or add additional context in comments.

Comments

0

It is possible that your events object's json formatting. Try the following:

You JSON string also seems to be incorrect. The field description should be quoted as well as the answers. remove the comma after the description data

 [{
            "name": "event1",
            "description": "description 1"
        },
        {
            "name": "event2",
            "description": "description 2"
        }
    ]

In some cases due to the JSON formatting you might have an object containing an array:

<a href="#" class="list-group-item" v-for="(event, key, index) in events.**events**"> //events can be .data or events[0]
    index is {{index}}
</a>

DEMO

1 Comment

great! try and use this tool to validate your JSON - makes life a lot easier... jsonformatter.curiousconcept.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.