In the scenario of having an array like this:
people = [
{
name: 'Bob',
sex: 'male',
address:{
street: 'Elm Street',
zip: '12893'
}
},
{
name: 'Susan',
sex: 'female',
address:{
street: 'Hickory Street',
zip: '00000'
}
}
]
I want to create a function that can modify specific instances of '00000' in the nested field 'zip' to '12893' and generate a new array with the corrected values. Here is my current attempt at a function:
function zipFix (initialArray) {
return initialArray.map(function(person) {
if(person.address.zip === '00000')
person.address.zip = "12893"
return person
});
}
Although this function is altering the values in 'initialArray', which is not the intended behavior. How can I adjust my function so that I can correctly use the map function to produce a new array with the corrections? Thank you.