I have a selectedItem object in Angular, it contains other objects and arrays. I am creating a deep copy using the JSON trick:
$scope.editableItem = JSON.parse(JSON.stringify($scope.selectedItem))
Then I use the editableItem model in the inputs, change some values ββinside. selectedItem does not change. Then I want to send through PATCH all the changes made, but not the fields that have not been changed. Therefore, I need to remove editableItem from all fields that are the same in unchanged selectedItem .
How to do it efficiently? I was thinking of traversing an object recursively using Underscore, but I would really like to know whether it is good to think before I solve it.
Alternatively, I could create a third object that will contain only indirect fields from the second, added dynamically, but I'm not sure how to approach this.
Editorial: To be clear, I expect the answer to be general and suppose that the most complex structure of the object is possible. For example, there are no answers from this question , because they either assume that the object has only simple fields, or they must have an Angular observer explicitly set for each field separately.
source share