我有一个我想根据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