首页 > 解决方案 > 使用 js operator ... 将数据传递到 redux reducer

问题描述

我想将嵌套对象传递给 redux reducer,我知道我必须使用 ... 运算符来进行深层复制。但我不知道如何传递/复制“评论”数组和嵌套在“评论”数组的每个元素中的“_user”对象。

{
"mess": "",
"article": {
    "_id": "5faba634513b091effd8c37f",
    "article": "intoheadLifestyle",
    "likeArt": 0,
    "dislikeArt": 0,
    "comments": [
        {
            "_id": "5faba635513b091effd8c381",
            "commentId": {
                "_id": "5faba634513b091effd8c380",
                "comment": "finish for today, all is working as expected, even better .....",
                "like": 0,
                "dislike": 0,
                "_user": {
                    "_id": "5fa93d08533de7437c77a5a0",
                    "username": "jerome",
                    "password": "$2b$10$is66cqM5K5ZE/z7T34a54e0TNXSrdVZIx7jF5sqkZQ1gAwOplt4Si",
                    "__v": 0
                },
                "createdAt": "2020-11-11T08:52:04.960Z",
                "__v": 0
            }
        },
        {
            "_id": "5faba64b513b091effd8c383",
            "commentId": {
                "_id": "5faba64a513b091effd8c382",
                "comment": "another coent ca c bien coool",
                "like": 0,
                "dislike": 0,
                "_user": {
                    "_id": "5fa93d08533de7437c77a5a0",
                    "username": "jerome",
                    "password": "$2b$10$is66cqM5K5ZE/z7T34a54e0TNXSrdVZIx7jF5sqkZQ1gAwOplt4Si",
                    "__v": 0
                },
                "createdAt": "2020-11-11T08:52:26.995Z",
                "__v": 0
            }
        }
    ],
    "__v": 2
}

}

这个对象来自“res.payload.data”,所以我试着这样做

let datas = {...res.payload.data,
                  article: {...res.payload.data.article,
                           // then i don t know for the 'comments' array ..???
                           // and i am lost for the '_user' object into each element of the array...

如果有人有想法,谢谢。

标签: reactjsreduxreact-redux

解决方案


好的,我了解如何传递评论[],谢谢。

但是如何将'_user'对象传递到'comments'数组的每个元素中,它也需要传播(我不知道我们是否这样说。)以及......


推荐阅读