Iterating object properties

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!

1 Like

Hi!

You could try using the Update Object action node instead.

If I understand your use case it would be possible to solve this by using an Update object that has a filter (=object A). In the function editor you can add the object class as a function parameter and filter that on object B and return the properties.

Let us know if this helps.

Hi Shahitha,

If you were to use Update Object, I would have to bind every property individually which is too time-consuming when there are many properties. Or is there another way I am not seeing?

That is the only way to properly update the objects. Hopefully just a one-time thing!