Saturday, 4 March 2017

javascript - How do you assign a value inside a forEach loop?

I expected the first console.log to be [0,10,20,30] and the second to be [0,100,20,30]:





var arr = [0,10,20,30]
console.log(arr)

arr.forEach(each)
console.log(arr)

function each(data,index) {
if (index === 1) {
data = 100 // This isn't assigned to arr[1]
}
}


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...