let arr = [] arr.push(1) let arr2 = [2,3,4,5] arr2.forEach((item,index,arr)=>{ arr.push(item) console.log(arr) })
The result is
[2, 3, 4, 5, 2] [2, 3, 4, 5, 2, 3] [2, 3, 4, 5, 2, 3, 4] [2, 3, 4, 5, 2, 3, 4, 5]
jsbin address
https://jsbin.com/papamadejo/...
I want to know why this is the result
It shouldn’t be [1,2,3,4,5] What
The arr variable in foreach has the same name, so arr2 is operated.
Delete the third parameter (arr)
That’s it, you can refer to the instructions on Yiha mdn:
in this code
arr points to arr2.
You can make the following modifications