如果我有book
具有以下结构的对象
{title: string, author: string, genre: string, read: boolean}
我的req.body
包含book
对象以及其他几个的所有字段。
有没有办法重构以下代码以更简洁?
book.title = req.body.title
book.author = req.body.author
book.genre = req.body.genre
book.read = req.body.read
答案 0 :(得分:-2)
// req.body = { otherThing: 'something', title: 'ok', author: 'aut', genre: 'scifi', read: false, foo: 'bar', bar: 'foo' }
const { title, author, genre, read } = req.body
const book = { title, author, genre, read }
console.log(book) // { ... }