This afternoon I was practicing a bit with vuejs. Simply use axios to get some data. I succeeded in retrieving data. But then I encountered a problem with the for loop.
If I do this
<template></template>
<script>
export default {
data: function() {
return {
arr: []
}
},
created() {
axios.get('url')
.then(function(res) {
for (var i = 0; i <= res.length; i++) {
if (true) {
// If i assign the value to the 'array' here, the for loop will stop
this.arr.push('something')
// I have also tried this.$data.arr
}
}
})
.catch(err=>console.log(err));
}
}
</script>
So I changed a bit and it worked
<script>
export default {
data: function() {
return {
arr: []
}
},
created() {
var second_arr = []; // Defined a variable here
axios.get('url')
.then(function(res) {
for (var i = 0; i <= res.length; i++) {
if (true) {
// Working with 'second_arr' variable instead of the 'arr' variable in the data
second_arr.push = something;
}
}
})
.catch(err=>console.log(err));
//then assign 'second_arr' to 'arr' in the data
this.arr = second_arr;
}
}
</script>
The question is
1. In the first code, why does the loop only run once?
2. I have tried to find out and found a few comments about 'scope'. So can someone explain more clearly to me? Thanks.

res? like console logged it?