javascript scoping hell
How can i ensure that the local scope of a function in for each doesn't modify the original function
JavaScript
var gloabalList = [{n:0},{n:0},{n:0}];
var output = [];
gloabalList.forEach((item,index)=>{
gloabalList[index].n = index;
//at this point the value n is the index
messUpItem(item);
//at this point n is 2 we want it to be the index
output.push(item.n);
});
//we want this to be (1,2,3)
alert(output);
function messUpItem(item){
var x = item;// this should copy object val but its grabbing a reference
x.n = 2;
return item;
}