for (var key in arr) {
if (arr.hasOwnProperty(key)) {
console.log('这一次可以输出key'+key)
this.$http.post('/getPaperListByCIdAndTId', {
teacherId: window._const.teacherId,
}).then((res_in) => {
console.log('这一次不能输出key'+key)
})
}
}
The second output is $remove
Or tell me how to get the key in .then
Keyword: closure
It will be more elegant to use array.map to solve it
It’s closed. Just replace var with let in es6
This problem is a typical loop variable scope problem.
then()
中的回调被调用的时候key
可能已经循环到最后一个了(也可能是间的某个值),所以里面使用的key
值是当时的key
值。这在 ES6 中要可以用let
代替var
to solve it (because I see that you have already used the arrow function of ES6, so use ES6 first)If you want to write ES5, you can use an IIFE to seal the localized key value (passed in through parameters, so it will not change)
Recommended functional writing method, it looks simpler, ES6 can do it like this
ES2017 can also use async, the syntax is more concise
I just tested it, it works, and you must use let instead of var, otherwise the output will be the last key