我有一個我想根據ID過濾的註釋數組,並更改特定的鍵/值對。我的控制檯日誌函數返回數組中的正確項目,但我不確定使用.filter函數的結果並將'喜歡'鍵從'false'更改爲'true'的正確語法。如果適合這種情況,我可以使用擴展運算符。Redux Reducer - 基於ID的過濾器數組,然後更改鍵/值對
import * as types from '../actions/actionTypes';
import initialState from './initialState';
export default function commentReducer(state = initialState.comments, action) {
switch (action.type) {
case types.ADD_COMMENT_LIKE_SUCCESS:
const content = Object.assign({}, state);
const likedComment = content.data.filter(comment => (
comment.id === action.commentId
));
console.log(likedComment[0]);
break;
default:
return state;
}
}
的評論對象是這樣的:
"data":{
"data":[
{ id: '', comment: '', liked: false },
{ id: '', comment: '', liked: false },
{ id: '', comment: '', liked: false },
{ id: '', comment: '', liked: false }
],
"cursor":"CkUKEQoEZGF0ZRIJCNrwhcWhvdUCEixqFGRldn5kZXZlbG9wbWVudC0xMzAwchQLEgdDb21tZW50GICAgLSe_-cKDBgAIAE=",
"more":false,
"count":4
},
你的'initialState'是怎麼樣的? – JoseAPL
編輯我的問題,包括它。 – GuerillaRadio