如何将新值推送到 JavaScript 对象数组

How can I push new values to a JavaScript object array

我有一个包含数组的 属性 对象,我想知道插入该数组的最佳方法是什么,这是对象:

const object1 = 
  {"users": [{"username":"john","email":"john@gmail.com","id":"john_2011221"}, 
  {"username":"steve","email":"steve@gmail.com","id":"steve_201193841"}]};

我想把对象做成这样:

const object1 = 
      {"users": [{"username":"john","email":"john@gmail.com","id":"john_2011221"}, 
      {"username":"steve","email":"steve@gmail.com","id":"steve_201193841"}
      {"username":"Emily","email":"emily@gmail.com","id":"emily_20119567"}]};

使用Array#push.

object1.users.push({"username":"Emily","email":"emily@gmail.com","id":"emily_20119567"});

使用 object1["users"] 访问对象内的数组。然后你可以像这样简单地将你的新对象推入这个数组

const object1 = 
  {"users": [{"username":"john","email":"john@gmail.com","id":"john_2011221"}, 
  {"username":"steve","email":"steve@gmail.com","id":"steve_201193841"}]};

object1["users"].push({"username":"Emily","email":"emily@gmail.com","id":"emily_20119567"})