【问题标题】:push new array of object into existing array of object using spread of ES6使用 ES6 的扩展将新的对象数组推送到现有的对象数组中
【发布时间】:2018-07-08 08:44:14
【问题描述】:

这种语法是否正确? results 是我的对象数组,它是嵌套的。

notifications: {
  ...state.notifications,
  results: [
    ...state.notifications.results,
    { ...state.notifications.results }
  ]
}

【问题讨论】:

  • 是的,这可能是有效的,尽管这看起来很奇怪
  • Is this syntax even correct? 是的
  • 请添加想要的结果(和state.notifications)。在对象中展开数组很奇怪,你会得到一个以索引为键的对象,但如果需要,也可以。

标签: javascript reactjs ecmascript-6 redux


【解决方案1】:

在我看来,您正在尝试运行 this.setState,并在这样做的同时更新您的数组。

完整的命令是这样的:

this.setState({
  notifications: {
    ...this.state.notifications,
    results: [
      ...this.state.results,
      // whatever you're trying to add goes here
    ]
  }
});

我只是根据您问题上的标签进行猜测。请随时发表评论并进一步解释您的情况,以便我可以调整我的答案以更好地满足您的问题。

【讨论】:

    【解决方案2】:

    这应该可行。

    notifications: {
      ...state.notifications,
      results: [
        ...state.notifications.results
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 2022-06-14
      • 2021-07-20
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多