有没有更简单的方法来为这个变量分配对象解构?

Is there an easier way to assign this variable with object destructuring?

const user = new db.User({
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        password: req.body.password,
        email: req.body.email,
        dateCreated: req.body.dateCreated
    })

我知道有一种方法可以通过为属性赋予与其来源相同的名称来为对象赋值,但我不确定这将如何工作。

您可以解构以上所有这些值以简化它:

const { firstName, lastName, password, email, dateCreated } = req.body

那么您需要做的就是:

const user = new db.User({
    firstName,
    lastName,
    password,
    email,
    dateCreated,
})

这似乎是 Automapper 的一个很好的用例。它将像名称一样映射并且可以折叠变量。
http://automapper.org/

你也可以把它作为一个功能性的单行,但即使在这种情况下你仍然需要重复 属性 个名字。

const user = new db.User(
  (({firstName, lastName, password, email, dateCreated}) =>
   ({firstName, lastName, password, email, dateCreated}))(req.body)
);