2017-06-14 57 views
0

這是我的刪除代碼,它只適用於第一次調用?我的刪除操作無法正常工作?

const byId = (state = {}, action) => { 
    switch (action.type) { 
    case 'REMOVE_TODO': 
     return Object.keys(state).filter(key => { 
     return state[key].id !== action.id 
     }).reduce((acc,key) => Object.defineProperty(acc, key, { 
     value: state[key], }),{});   
      default: 
     return state; 
    } 
}; 

我的狀態對象是這樣的:

{"abcd1": {id: "abcd1", text: "hello world"}, "efgh2": {id: "efgh2", text: "hello there"} 

所以會發生什麼是我可以刪除第一invokation,那麼我的對象成爲所有空當我試圖再次調用?或僅{}。無法弄清楚爲什麼?因爲它只是多餘的,如果它們是相同的重複對象鍵和待辦事項ID

[{id: "abcd1", text: "hello world"},{id: "efgh2", text: "hello there"}] 

回答

0

嗯,我寧願塑造我的狀態對象等。 然後,在減速機,你可以有這樣的東西:

const byId = (state = [], action) => { 
    switch (action.type) { 
    case 'REMOVE_TODO': 
     return state.filter((todo) => todo.id != action.id) 
    default: 
     return state; 
};