Javascript 使用redux将对象添加到数组内部的数组
您好,我的问题是我不会在父数组id中的operationId找到的数组[notes]中创建、更新或删除{note}。但我现在不想这样做。我有ADDNOTE,但它不工作。 我写我放在这里只添加注释,因为这些我不能开始删除和更新 我的默认Javascript 使用redux将对象添加到数组内部的数组,javascript,json,reactjs,redux,react-redux,Javascript,Json,Reactjs,Redux,React Redux,您好,我的问题是我不会在父数组id中的operationId找到的数组[notes]中创建、更新或删除{note}。但我现在不想这样做。我有ADDNOTE,但它不工作。 我写我放在这里只添加注释,因为这些我不能开始删除和更新 我的默认 const initialState = { loading : false, isLoaded: false, person : { id:'', firstName: '', operat
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],
}
),
},
};
有关如何更新的更多信息,请参见是否执行此操作?