The code provided may not achieve the desired outcome. Consider the following example...
const default = { x: 1 };
const selectionA = { y: 2 };
const selectionB = { x: undefined, z: 3 };
return {
...default,
...selectionA,
...selectionB
};
If your goal is to prioritize selectionB over selectionA if it's not null, this objective will not be met. The resulting object will be { x: undefined, y: 1, z: 3 }
. The default value of x gets replaced by undefined from selectionB.
If you aim to return properties from a non-nullish object in a specified order, consider using the null coalescing operator.
return {
...(layerTreeNode?.pathAttributesSelection
?? parentTreeNode?.pathAttributesSelection
?? defaultPathAttributes)
};
Even with the above example, it may not align precisely with your specifications. If the property pathAttributesSelection within layerTreeNode is nullish, it will move on to the next variable.
If the above scenario doesn't meet your requirements, utilize conditional ternary or other similar structures.
return {
...(layerTreeNode ? layerTreeNode.pathAttributesSelection
: parentTreeNode ? parentTreeNode.pathAttributesSelection
: defaultPathAttributes)
};
In conclusion, clarity in your objectives is crucial as changes in logic can lead to slightly different outcomes despite similar appearances.