javascript - Object.assign正在修改原始数组对象

标签 javascript arrays reactjs

我正在尝试修改我所在州的特定索引的属性值,该属性是 post_comments 但我的问题是状态正在被修改,即使我只是修改它的副本..代码按照我想要的方式工作,但我正在修改状态,所以它可能很糟糕,我该如何解决这个问题?

socket.on('statusComment', (data) => {
    const mutator =  Object.assign([], this.state.getStatus);
    const index =  mutator.findIndex(i => i._id === data._id);
    mutator[index].post_comments = data.post_comments; // Replace old post_comments with data.post_comments 
    console.log(mutator) // Got the post_comments 
    console.log(this.state.getStatus) // Also modified 

    // Commented out setState
    // this.setState({
    //     getStatus: mutator
    // })
});

这是套接字检测到的示例数据

const data = {
  post_id: "5b0689f03fb2fd1404f1854d",
  post_comments: [{text: 'what'}]
}

这就是我的状态

   const data_arr = [
      {
        "post_img": [],
        "post_date": "2018-05-24T09:46:24.948Z",
        "post_comments": [
          {
            "comment_posted": "2018-05-24T09:46:31.015Z",
            "_id": "5b0689f73fb2fd1404f1854e",
            "comment_from": {
              "photo_url": "png",
              "_id": "5af16d60f3957c11e46500ae",
              "display_name": "Lumpo"
            },
            "comment_text": "kaka2"
          },
          {
            "comment_posted": "2018-05-24T09:47:42.752Z",
            "_id": "5b068a3e2fdd6f141d5ba995",
            "comment_from": {
              "photo_url": "png",
              "_id": "5af16d60f3957c11e46500ae",
              "display_name": "Lumpo"
            },
            "comment_text": "kaka!"
          }
        ],
        "_id": "5b0689f03fb2fd1404f1854d",
        "post_description": "get out\r\n",
        "post_by": {
          "photo_url": "png",
          "_id": "5af16d60f3957c11e46500ae",
          "display_name": "Lumpo"
        },
        "__v": 2
      }
    ]

Spread 运算符不起作用,使用 Object.assign 方法记录相同的内容 //console.log(mutator)

[
  {
    "post_img": [],
    "_id": "5b0694cc7925c914e4d95dda",
    "post_description": "test",
    "post_by": {
      "_id": "5af16d60f3957c11e46500ae",
      "display_name": "Lumpo",
      "photo_url": "png"
    },
    "post_comments": [
      {
        "_id": "5b0694d67925c914e4d95ddb",
        "comment_from": {
          "photo_url": "png",
          "_id": "5af16d60f3957c11e46500ae",
          "display_name": "Lumpo"
        },
        "comment_text": "This comment should only be in the mutator ",
        "comment_posted": "2018-05-24T10:32:54.937Z"
      }
    ],
    "post_date": "2018-05-24T10:32:44.613Z",
    "__v": 0
  }
]

//console.log(this.state.getStatus);

[
  {
    "post_img": [],
    "_id": "5b0694cc7925c914e4d95dda",
    "post_description": "test",
    "post_by": {
      "_id": "5af16d60f3957c11e46500ae",
      "display_name": "Lumpo",
      "photo_url": "png"
    },
    "post_comments": [
      {
        "_id": "5b0694d67925c914e4d95ddb",
        "comment_from": {
          "photo_url": "png",
          "_id": "5af16d60f3957c11e46500ae",
          "display_name": "Lumpo"
        },
        "comment_text": "This comment should only be in the mutator ",
        "comment_posted": "2018-05-24T10:32:54.937Z"
      }
    ],
    "post_date": "2018-05-24T10:32:44.613Z",
    "__v": 0
  }
]

最佳答案

const mutator =  Object.assign([], this.state.getStatus);

它正在执行数组的浅/引用副本。

因此,原始数组在使用引用时被复制。

使用spread operator创建数组的新副本,然后执行 JSON.stringify,然后执行 JSON.parse。U 需要一个深拷贝。

let mutator = [...this.state.getStatus];
mutator = JSON.parse(JSON.stringify(mutator));

关于javascript - Object.assign正在修改原始数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50506789/

相关文章:

c# - 如何在不知道要存储多少值的情况下创建数组?

java - 按照 int 数组给定的顺序打印 String 数组

编译器警告涉及数组的不匹配函数声明

javascript - react JS : Problems with passing my handler function with props

javascript - "Found @client directives in query but no client resolvers were specified"使用客户端缓存时出现警告

javascript - 通过 javascript 验证文本区域

javascript - 使用 moment.js 根据日期更改输入字段的背景和颜色

javascript - 数据未通过 XMLHttpRequest 发送

javascript - 将字符串拆分为参数,就像 JavaScript 中的 URL 一样

reactjs - React fullcalendar错误无法读取未定义的属性 'seg'