I am using the below function to decide if I have new value for a key in my json doc or not. If I do, I go and use the new value, otherwise I use the previously stored value. This works fine if it is a direct value I am referencing, but for example if i use
home_addr.address_line_2 =
updateValue(oldData.home_address.address_line_2,
data.home_address_line_2)
and oldData.home_address does not exists it complains that address_line_2 is undefined. So how can I handle this in a way without having to check if each parent object is created to avoid this error ?
const updateValue = (oldvalue, newvalue) => {
if (typeof newvalue !== "undefined" && newvalue !== null) {
console.log("new Value to trim : " + newvalue)
if (typeof newvalue == "boolean") {
return newvalue;
} else if(typeof value === 'number') {
return newvalue;
}
else {
return newvalue.trim();
}
} else {
if (typeof oldvalue !== "undefined" && oldvalue !== null){
return oldvalue;
} else {
return null
}
}
};