I am currently working on a Vue.js component that has a simple template.
<div @click="createTargets(2)">
text
</div>
Here is the script file for the component:
export default {
name: 'test',
data() {
return {
targets: [],
};
},
methods: {
createTargets(targetCount) {
this.targets = [];
var emptyTarget = {
id: null,
};
for (var i = 0; i < targetCount; i++) {
var targetToPush = emptyTarget;
targetToPush.id = i;
console.log(targetToPush.id);
this.targets.push(targetToPush);
console.log(this.targets);
}
return {};
},
},
}
After clicking on the text in the component, I noticed an unexpected output:
0
[{"id":1},{"id":1}]
1
[{"id":1},{"id":1}]
I am puzzled by this behavior and cannot seem to find the cause.
My expected output should be:
0
[{"id":0}]
1
[{"id":0},{"id":1}]
Any suggestions or ideas on why this is happening?