使用redux将Object添加到数组内部的数组中



你好,我的问题是我不会在父数组id中的operationId找到的数组[注释]中创建、更新或删除{注释}。但我现在不想这么做。我有ADDNOTE,但它不起作用。我在这里只写了ADDNOTE,因为有了这些,我不能启动DELETE和UPDATE 事件

我的默认

const initialState = {
loading : false,
isLoaded: false,
person : {
id:'',
firstName: '',
operations:[],
},
error : ''
};

my code : 
case ADDNOTE: return {
...state,
person:
{
...state.person,
operations:[
]
state.person.operations.find(item => item.id === action.payload.operationId).notes.push(action.payload)
}
}; 

操作.加载

{
"id": "22",
"operationId" : "123A",
"note": "bla bla"
}

我的习惯:

{
"loading" : false,
"person" : {
"id": ""  ,
"firstName": "",
"operations":[
{
"id" : "123A",
"notes": [
{
"id": "11",
"operationId" : "123A",
"note": "bla"
},
{
"id": "22",
"operationId" : "123A",
"note": "bla bla"
}
]
},
{
"id" : "456B",
"notes": [
{
"id": "99",
"operationId" : "456B",
"note": "bla xxx"
}
]
}
]
},
"error" : ""
}

我认为以下方法可行:

case ADDNOTE: return {
...state,
person: {
...state.person,
operations: state.person.operations.map((operation) =>
operation.id !== action.payload.operationId
? operation //not this operation, just return original
: { // add note
...operation,
notes: [...operation.notes, action.payload],
}
),
},
};

关于如何更新的更多信息可以在这里找到

相关内容

  • 没有找到相关文章

最新更新