Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更新减速器中的特定嵌套项?_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 如何更新减速器中的特定嵌套项?

Javascript 如何更新减速器中的特定嵌套项?,javascript,reactjs,redux,Javascript,Reactjs,Redux,我有一个具有以下结构的减速器: { "id": 8, "location_name": "Some place", "lat": "nn.nnnnnn", "lng": "-nn.nnnnn", "review_count": 3, "score_avg": 3.00,

我有一个具有以下结构的减速器:

{
    "id": 8,
    "location_name": "Some place",
    "lat": "nn.nnnnnn",
    "lng": "-nn.nnnnn",
    "review_count": 3,
    "score_avg": 3.00,
    "reviews": [
        {
            "id": 14,
            "userId": 1,
            "userName": "JohnDoe",
            "body": "Lorem Ipsum.",
            "score": null,
            "likes": 2,
            "liked": true,
            "review_pic": null
        },
        {
            "id": 15,
            "userId": 1,
            "userName": "JohnDoe",
            "body": "Lorem Ipsum.",
            "score": null,
            "likes": 0,
            "liked": false,
            "review_pic": null
        },
        {
            "id": 13,
            "userId": 4,
            "userName": "JohnDoe",
            "body": "Lorem Ipsum.",
            "score": null,
            "likes": 0,
            "liked": false,
            "review_pic": null
        }
    ]
}
另一方面,我有一个返回ID的action creator。我试图获取此ID以将喜欢更改为true并将喜欢设置为+1,但此reducer状态的嵌套特性让我感到困惑

编辑:以下是我一直在用我的减速机尝试的东西,但运气不好:

import {GET_REVIEWS, CLEAR_REVIEWS, LIKE} from './reviewTypes';

const initialState = [];

const reviewReducer = (state = initialState, action) => {
    switch(action.type) {
        case GET_REVIEWS:
            return action.payload;
        case LIKE:
           const reviews = state.reviews.map(review => {
               if(review.id === action.payload) {
                   return {
                       ...review,
                       liked: true,
                       likes: review.likes++
                   }
               } else {
                   return review;
               }
           });
           return {
               ...state,
               reviews
           }
        case CLEAR_REVIEWS:
            return [];
        default:
            return state;
    }
}

export default reviewReducer;

我迭代array.reviews以查找具有所需id的评论。如果我找到了它,我将增加likes并将liked设置为true

函数更新类(id,数组){
array.reviews.some(review=>{
if(review.id==id){
喜欢++;
review.like=true;
返回true;
}
});
}
让数组={
“id”:8,
“地点名称”:“某个地方”,
“lat”:“nn.nnnnnn”,
“液化天然气”:“-nn.nnnnn”,
“审查计数”:3,
“平均得分”:3.00,
“审查”:[
{
“id”:14,
“用户ID”:1,
“用户名”:“约翰多”,
“主体”:“Lorem Ipsum.”,
“分数”:空,
“喜欢”:2,
“喜欢”:真的,
“查看图片”:空
},
{
“id”:15,
“用户ID”:1,
“用户名”:“约翰多”,
“主体”:“Lorem Ipsum.”,
“分数”:空,
“喜欢”:0,
“喜欢”:假,
“查看图片”:空
},
{
“id”:13,
“用户ID”:4,
“用户名”:“约翰多”,
“主体”:“Lorem Ipsum.”,
“分数”:空,
“喜欢”:0,
“喜欢”:假,
“查看图片”:空
}
]
};
类更新(15,数组);
console.log(数组)您可以使用来获取具有特定
id的元素

const data={“id”:8,“location_name”:“Some place”,“lat”:“nn.nnnnnn”,“lng”:-nn.nnnnn”,“review_count”:3,“score_avg”:3.00,“reviews”:[{“id”:14,“userId”:1,“userName”:“JohnDoe”,“body”:“Lorem Ipsum.”,“score”:null,“likes”:2,“liked”:true,“review_pic”:null},{“id”:15,“userId”:1,“userName”JohnDoe,“body”:“Lorem Ipsum.”,“score”:null,“likes”:0,“liked”:false,“review_pic”:null},{“id”:13,“userId”:4,“userName”:“JohnDoe”,“body”:“Lorem Ipsum.”,“score”:null,“likes”:0,“liked”:false,“review_pic”:null};
设id=13;
让obj=data.reviews.find(x=>x.id==id);
如果(obj)obj.like=true;

console.log(data);
您不必在该数组上使用reducer,因为您只有一个数组,并且您应该可以使用useState,这段代码应该适合您

const onItemLike = (itemID) => {
          setState(state.map(x => {
               if(reviews.id === itemID)
               return {...x, liked: true, likes: state.reviews[x].likes+1}
          }))
     }