Mik*_*ger 2 javascript node.js data-structures express
我的Node.js/Express网络应用程序中有如下的JavaScript数据结构:
var users = [
{ username: 'x', password: 'secret', email: 'x@x.com' }
, { username: 'y', password: 'secret2', email: 'y@x.com' }
];
Run Code Online (Sandbox Code Playgroud)
收到新用户的已过帐表单值后:
{
req.body.username='z',
req.body.password='secret3',
req.body.email='z@x.com'
}
Run Code Online (Sandbox Code Playgroud)
我想将新用户添加到数据结构中,这应该导致以下结构:
users = [
{ username: 'x', password: 'secret', email: 'x@x.com' }
, { username: 'y', password: 'secret2', email: 'y@x.com' }
, { username: 'z', password: 'secret3', email: 'z@x.com' }
];
Run Code Online (Sandbox Code Playgroud)
如何使用发布的值向我的用户数组添加新记录?
您可以使用push方法将元素添加到数组的末尾.
var users = [
{ username: 'x', password: 'secret', email: 'x@x.com' }
, { username: 'y', password: 'secret2', email: 'y@x.com' }
];
users.push( { username: 'z', password: 'secret3', email: 'z@x.com' } )
Run Code Online (Sandbox Code Playgroud)
你也可以设置,users[users.length] = the_new_element但我不认为这看起来不错.