2016-11-22 85 views
0

我有一個不變的地圖對象,一個Map對象中,如下所示:更新地圖內的地圖

let initialState = Immutable.fromJS({errors:{}}); 

如何更新錯誤圖,以添加和刪除條目?

例如:

來自:

Immutable.fromJS({errors:{}}) 

到:

Immutable.fromJS({errors:{"foo":"My foo error"}}) 

到:

Immutable.fromJS({errors:{"foo":"My foo error", "baz": "A baz error"}}) 

到:

Immutable.fromJS({errors:{"baz": "A baz error"}}) 

回答

1

您可以使用setIndeleteIn

const initialState = Immutable.fromJS({errors:{}}); 
const nextState = initialState.setIn(['errors', 'foo'], 'bar').setIn(['errors', 'bar'], 'a bar error'); 
console.log(nextState.toJS()); 
const finalState = nextState.deleteIn(['errors', 'bar]); 
console.log(finalState.toJS()); 

輸出:

{ 
    errors: { 
     foo: "a foo error", 
     bar: "a bar error" 
    } 
} 
{ 
    errors: { 
     foo: "a foo error" 
    } 
} 

```