My code is :
let obj1 = [
{ city: 'Tokyo', country: 'Japan' },
{ city: 'Bangkok', country: 'Thailand'}
];
function myFunc(arr, key, value){
let newArr = arr.map((e)=>{
let obj = e;
obj[key] = value;
return obj;
})
return newArr
}
console.log(myFunc(obj1, 'continent', 'Asia'));
console.log(obj1);
I don’t want to mutate the “obj1” array. That’s why I’m using Array.map method. But it still changing the original Array. Does anyone know why this happening? Where I made the mistake?