var arr = ['Foo'];
arr.forEach(function(item){
console.log(item);
item = 'Lorem';
console.dir(arr[0]);
});
for (var item in arr){
arr[item] = 'Ipsum';
console.dir(arr[0]);
}
Like the code above shows, I noticed that changing the value of an item passed to the callback of forEach()
does not cause the iterated object to alter.
Using for...in
certainly does.
Why is that & how should I alter values in an array?
I find that the topic is covered quite confusing on MDN
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…