2017-01-25 49 views
0

我有一個對象數組,我將其提交給控制器之前進行轉換。對象分配深度複製導致問題

角代碼拷貝如下:

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

以下似乎工作:

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

,但它會導致以下問題。

Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: [email protected]; 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: [email protected]; line: 1, column: 1] 

我的控制器方法簽名是:

@RequestMapping(.., method=RequestMethod.POST) 
public save(@RequestBody List<Object>, BindResult bindResult){} 
+0

爲什麼你認爲這是一個副本? ['Object.assign'](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)突變了它的第一個參數。 – jonrsharpe

+0

我只是想在angular1 angular.copy中做深度複製對象。你能建議嗎? –

+0

請將「SourceObject」的代碼添加到問題 –

回答

0

參考下面的帖子,發現下面的代碼片段工作

How can i use angular.copy in angular 2

duplicateObject = <YourObjType> JSON.parse(JSON.stringify(originalObject)); 
+2

然後你應該把這篇文章標記爲重複而不是複製答案。這是一個20k +用戶的基本SO東西! – jonrsharpe