【问题标题】:JS Object Update + React ReducerJS 对象更新 + React Reducer
【发布时间】:2022-01-25 07:49:51
【问题描述】:

我正在努力将所有“新”属性更新得太假。下面是一个示例对象。

const msgdata = {
"1511207758": {
    "userid": "1000015977",
    "text": "hello",
    "new": false,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2017-11-20T19:55:58.000Z"
},
"1511277428": {
    "userid": "1000000000",
    "text": "hey hi",
    "new": false,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2017-11-21T15:17:08.000Z"
},
"1640341426": {
    "userid": "1000000000",
    "text": "hellow how are you?",
    "new": false,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2021-12-24T10:23:45.706Z"
},
"1640342296": {
    "userid": "1000000000",
    "text": "asdfasdf",
    "new": true,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2021-12-24T10:38:16.089Z"
},
"1640342382": {
    "userid": "1000000000",
    "text": "fxvfv",
    "new": true,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2021-12-24T10:39:41.910Z"
}

}

当我尝试以下操作时,我将对象结构更改为数组,但我需要维护对象/键结构 - 只需将新值更改为 false。

    let newMessages = {}
    if (payload.length > 0) {
        payload.map((item) => {
            if (messagesData && messagesData[item.userid]) {
                newMessages = Object.keys(messagesData[item.userid].messages).map((key) => ({ ...newMessages[item.userid].messages[key], new: false }))
            }
            return true
        })

        console.dir('newMessages')
        console.dir(newMessages)

返回对象是一个标准数组 - map 是这样做的...例如:0,1,2 个键...

如何维护对象结构并只更改新属性。

谢谢

【问题讨论】:

  • 使用 Object.entries(msgdata).reduce((acc, [key, value]) => { ... }, {}); 并在函数内部,执行 acc[key] = { ...value, new: false }; 设置副本的键,然后执行 return acc; (对于任何想知道的人,不要明显和故意将此作为答案发布)

标签: javascript reactjs


【解决方案1】:

您可以 map() 对象条目,将 new 值更改为 false 并返回条目以从中创建新对象。

const msgdata = {
  "1511207758": {
    "userid": "1000015977",
    "text": "hello",
    "new": false,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2017-11-20T19:55:58.000Z"
  },
  "1511277428": {
    "userid": "1000000000",
    "text": "hey hi",
    "new": false,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2017-11-21T15:17:08.000Z"
  },
  "1640341426": {
    "userid": "1000000000",
    "text": "hellow how are you?",
    "new": false,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2021-12-24T10:23:45.706Z"
  },
  "1640342296": {
    "userid": "1000000000",
    "text": "asdfasdf",
    "new": true,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2021-12-24T10:38:16.089Z"
  },
  "1640342382": {
    "userid": "1000000000",
    "text": "fxvfv",
    "new": true,
    "subjecttitle": null,
    "subjectcontent": null,
    "datetime": "2021-12-24T10:39:41.910Z"
  }
}

const result = Object.fromEntries(Object.entries(msgdata).map(([k, v]) => {
  return [k, { ...v, new: false }]
}));

console.log(result);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2018-06-03
    • 1970-01-01
    • 2021-01-08
    相关资源
    最近更新 更多