J. Doe J. Doe - 1 month ago 9
Javascript Question

How is my array that I am not using getting updated?

Here is my function:

function RemoveOutputKeys(array){
var temp = array;
for(var object in temp){
delete temp[object]['statusCode']
delete temp[object]['statusResponse']
}
console.log(array)
if(temp == array)
console.log("how is this possible?!?!!?!?!")
return temp
}


and here is the input I am providing,

array = [{'statusCode':400},{'statusCode':200}]


It makes sense for
temp
to get updated but I don't want the
array
to get updated. How can i fix this issue?

Thanks

Answer

If you want create new array instead of alias/reference use:

var newArray = oldArray.slice();