Hi!
I would like to ask about whether it is possible to iterate object properties. This is especially useful when an object has many properties. It would be both faster and less prone to mistakes.
I have a specific use case where I want to update all properties of object A with object B (of the same type), provided that object B has a non-null value for the corresponding property. This is the code I have written:
function updatePropertiesIfNotNull(target, updates) {
if (updates.length !== 1) {
throw new Error(βThe updates list must have exactly one object.β);
}
const updateObject = updates[0]; // Extract the single object
for (const key in updateObject) {
if (updateObject[key] !== null && updateObject[key] !== undefined) {
target[key] = updateObject[key];
}
}
}
The problem here is that I cannot directly update a function parameter. Please advise, thanks!