我的主要對象是這樣的:如何使用Object.assign()分配嵌套對象?
const obj = {
data: {
'Puppies' : [],
'Kittens': []
}
};
我想在data.Puppies值分配給數據字段[0]。
當我試圖做到這一點使用Object.assign()我得到一個錯誤:
Unexpected token, expected , (83:12)
81 |
82 | return Object.assign({}, obj, {
> 83 | data.Puppies[0]: list
| ^
84 | });
85 | }
86 |
我不知道我怎麼能完成我的任務。 我需要使用Object.assign(),因爲我需要返回一個不是原始對象的新對象。我這樣做是因爲ReactJS中的Redux Reducer。
所以基本上cou需要從obj中獲得一個拷貝,並且在索引零處的'Puppies'中有一個新項目而不會改變原始對象,對嗎? –