0

I have an object array which I transform before submitting it to the controller.

Angular code to copy as follows:

    sourceObjArray: SourceObject[] = [..];
    targetObjArray: SourceObject[]= [];
    targetObjArray = object.assign(sourceObjArray);
   // when i change target object it also cause source object to change
    transformSourceObject(targetObjArray)

The following seems to be working:

targetObjArray = object.assign({}, sourceObjArray);
// when i call transform it does not effect source object :)
transformSourceObject(targetObjArray)

but it causes a the following problem.

Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.PushbackInputStream@302753d0; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.PushbackInputStream@302753d0; line: 1, column: 1]

My controller method signature is:

@RequestMapping(.., method=RequestMethod.POST)
public save(@RequestBody List<Object>, BindResult bindResult){}
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
d-man
  • 57,473
  • 85
  • 212
  • 296
  • Why do you think that's a copy? [`Object.assign`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) mutates its first argument. – jonrsharpe Jan 25 '17 at 20:22
  • i simply want to do as in angular1 angular.copy which deep copy the object. can you suggest ? – d-man Jan 25 '17 at 20:33
  • Please add the code for `SourceObject` to the question – Nitzan Tomer Jan 25 '17 at 20:36
  • I'd suggest research! http://stackoverflow.com/q/36124363/3001761, http://stackoverflow.com/q/34688517/3001761, ... – jonrsharpe Jan 25 '17 at 20:38
  • thanks for help got the ans from your post – d-man Jan 25 '17 at 20:40
  • Possible duplicate of [How can i use angular.copy in angular 2](http://stackoverflow.com/questions/34688517/how-can-i-use-angular-copy-in-angular-2) – d-man Jan 26 '17 at 15:49

1 Answers1

0

with reference to the following post, found following code snippet working

How can i use angular.copy in angular 2

duplicateObject = <YourObjType> JSON.parse(JSON.stringify(originalObject));
Community
  • 1
  • 1
d-man
  • 57,473
  • 85
  • 212
  • 296
  • 2
    Then you should mark this post as a duplicate rather than copy over the answer. This is basic SO stuff for a 20k+ user! – jonrsharpe Jan 25 '17 at 21:20