首页 > 解决方案 > 如何检查布尔值是否设置为 true,然后将其推送到嵌套数组

问题描述

此时评论将被推送到数组中,但随后我单击放置的评论,并在输入字段中放置一些文本并单击添加...它应该被推送到嵌套数组中。查看评论,它应该检查 enableReply 是否设置为 true,如果是这种情况,则应将新添加的评论推送到嵌套数组中的该索引中。所以我的问题是,我该怎么做。另一种可能的选择是首先检查 enableReplyComment 是否设置为 true,然后再深入到 comments 数组,并检查image.comments.enableReply是否设置为 true。

链接到代码沙盒

在此处输入图像描述

        case types.ADD_COMMENT: {
            console.log(state);
            return {
                ...state,
                imageData: state.imageData.map(text => text.id === action.id ?
                    {   
                        ...text, 
                        comments: [...text.comments, {comment: action.newComment, likeComment: text.toggle, enableReply: false, replyComments: [{}]}]
                    } : text
                ) 
            }
        }

标签: javascriptreactjsredux

解决方案


推荐阅读