【问题标题】:How to push a value inside an object? [duplicate]如何将值推入对象? [复制]
【发布时间】:2022-08-03 17:40:36
【问题描述】:

我正在开发一个 node.js 项目,而我正在尝试做的是在对象中推送一个值。 对象是:

  {
    phone: \'1234\',
    fullName: \'John\',
    _id: new ObjectId(\"62ea3f5e6e03a2ffec2ddb83\"),
    createdAt: 2022-08-03T09:26:54.298Z
  }

我试图添加到此对象的是活动/非活动状态。

  {
    phone: \'+1234\',
    fullName: \'John\',
    _id: new ObjectId(\"62ea3f5e6e03a2ffec2ddb83\"),
    createdAt: 2022-08-03T09:26:54.298Z,
    status: \"active\"  // the new value
  }

我尝试过的是:

  function addingProps() {
    console.log(clients) // its the object of the client
    clients.push({ status: \"active\"});
  }

这个函数返回给我这个:

[
  {
    phone: \'+1234\',
    fullName: \'John\',
    _id: new ObjectId(\"62ea3f5e6e03a2ffec2ddb83\"),
    createdAt: 2022-08-03T09:26:54.298Z,
  },
  {
    _id: new ObjectId(\"62ea3fa57aaa2790e17ca1ad\"),
    createdAt: 2022-08-03T09:28:05.890Z
  }
]

如何在对象内添加status

  • clients[0].status = \'active\'…?!

标签: javascript node.js


【解决方案1】:
function addingProps() {
    console.log(clients)
    clients.status= "active";
}

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 2016-12-25
    • 2015-01-23
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多