I have a function that takes the full-path(a.b.c) to a field in an object and deletes it, i pass the object as a parameter, part of the function is reassigning the object parameter which is a reference to the object being modified, but when i log the object, nothing has changed except for that one field being deleted, I reassign it's reference inside the function but it stays the same outside, why?
function deleteObjectKey(object, fieldPath) {
const fields = fieldPath.split('.');
for (let i = 0; i < fields.length; i += 1) {
const field = fields[i];
if (i === fields.length - 1) {
delete object[field];
}
if (object[field] === undefined) {
return;
}
object = object[field];
}
}
const obj = { a: { b: { c: 'hello' } } };
deleteObjectKey(obj, 'a.b.c');
console.log(obj);
// outputs: { a: { b: {} } }