JavaScript ES6内联对象文字创建逻辑

Jus*_*tin 1 javascript ecmascript-6

是否有重写以下ES6 JavaScript不需要临时存储变量data

const data = {};

if(error) {
    data.error = error;
} else {
    data.response = 'some response here';
}

res.json(200, data);
Run Code Online (Sandbox Code Playgroud)

我想能够将对象内联到res.json():

res.json(200, {
    // inline logic that is identical to above
});
Run Code Online (Sandbox Code Playgroud)

syn*_*t1c 7

你可以使用三元运算符

res.json(200, error ? {error} : {response: 'some response here'});
Run Code Online (Sandbox Code Playgroud)