The console.log on line 9 shows
{ 'count' : 1111111 , 'average' : 2222222 , 'total' : 3333333 }
for all 3 array elements even though the loop that makes those changes has not run yet. How is this possible?
function test11(){
var test = [
{ 'count' : 1 , 'average' : 2 , 'total' : 3 } ,
{ 'count' : 10 , 'average' : 20 , 'total' : 30 } ,
{ 'count' : 100 , 'average' : 200 , 'total' : 300 }
] ;
console.log( test ) ;
test.forEach( function( element , service_index , array ){
array[ service_index ].count = 1111111 ;
array[ service_index ].average = 2222222 ;
array[ service_index ].total = 3333333 ;
});
console.log( test ) ;
return ;
}
Here is a jsfiddle of the code http://jsfiddle.net/d46wh2cv/7/ .
I read the specs at :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
but I don't see any explanation for this counter intuitive behavior.
I am running Debian Linux with Google chrome 39.0.2171.95 and have also had the same result in Iceweasel 24.5.0.
Thanks for the help.