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