如果我用 "arr[items]" 替换 "arr[index]" 我得到一个值为 [ 2, 0, 3, 4 ] 的数组,有人可以解释发生了什么
If i replace "arr[index]" with "arr[items]" i get an array with the values of [ 2, 0, 3, 4 ], can someone explain what is happening
const ary = [1, 5, 3]
ary.forEach(mult)
function mult(items, index, arr) {
arr[items] = index * 2;
}
console.log(ary)
[1, 5, 3]
在开始items=1 index=0
arr[1]= 0*2 i,e 0
[1,0,3]
然后items变成0 index=1
arr[0]= 1*2 i,e 2
[2,0,3]
然后items变成3 index=2
arr[3]= 2*2 i,e 4
arr[2]保持不变
当 arr[items] 被修改时,items 也会改变
const ary = [1, 5, 3]
ary.forEach(mult)
function mult(items, index, arr) {
arr[items] = index * 2;
}
console.log(ary)
[1, 5, 3]
在开始items=1 index=0
arr[1]= 0*2 i,e 0
[1,0,3]
然后items变成0 index=1
arr[0]= 1*2 i,e 2
[2,0,3]
然后items变成3 index=2
arr[3]= 2*2 i,e 4
arr[2]保持不变
当 arr[items] 被修改时,items 也会改变