'Which is more optimum to use when adding keys to an object ? - Spread operator with && or ternary if-else operator
Assuming the first_name key is always present in the request body: which is more optimum , method 1 or method 2 ?
const user: any = {}
// Use ternary operator - 1
user.first_name = req.body.first_name ? req.body.first_name : user.first_name
//Or Spread operator with && - 2
user = {
...(req.body.first_name && {'first_name': req.body.first_name} )
}
user.save()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
