I have an array that is rendered as a set of Vue components using v-for
. I need to use :key=""
otherwise Vue complains.
My array has no unique identifiers so I use the index as the key
My loop
<card v-for="(item, index) in questions" :question="item" :key="index">{{item}}</card>
The array has objects that can have the same contents
[{name:"jimmy"}, {name:"billy bob"}, {name:"jimmy"}]
The problem: as soon as I manipulate the array the results become very unpredictable. Sometimes the components are rendered correctly. Sometimes a new component appears in the middle of the v-for
list even though it was pushed into the array. Sometimes, after a shift / pop, all the old components remain and the newly pushed one doesn't appear.
Code
if(this.questions.length > 4) this.questions.shift()
this.questions.push({name:"willy jim"})
If I use item.name
as the key, it works perfectly fine so long as there are no duplicate names. If I use item.name+index
or something so silly, the whole thing goes bonkers...