Javascript Node.js根据其值返回对象键
在nodejs 8.10中,我需要一个函数根据参数返回不同的对象。有没有简单的方法可以根据键的值包含或不包含键 范例Javascript Node.js根据其值返回对象键,javascript,node.js,Javascript,Node.js,在nodejs 8.10中,我需要一个函数根据参数返回不同的对象。有没有简单的方法可以根据键的值包含或不包含键 范例 // I do not like this solution, there is a 2 line body // `c` may be undefined or a string (for example) const f = (a, b, c) => { if (c) return ({ a, b, c }); else return { a, b } } 我
// I do not like this solution, there is a 2 line body
// `c` may be undefined or a string (for example)
const f = (a, b, c) => {
if (c) return ({ a, b, c });
else return { a, b }
}
我们可以根据c
的值进行简单的返回吗?
我期待这样的事情:
// I expect this kind of solution.
const f = (a, b, c) => ({ a, b, ___????___ })
你没有办法做到这一点,但你可以:
const f = (a, b, c) => (c ? { a, b, c } : { a, b });
或
你没有办法做到这一点,但你可以:
const f = (a, b, c) => (c ? { a, b, c } : { a, b });
或
您可以使用
参数
。请检查。您可以使用参数
。请检查。第一个解决方案重复了a
和b
,第二个仍然是两行答案。这不是我想要的。谢谢。第一个解决方案重复了a
和b
,第二个仍然是两行答案。这不是我想要的。谢谢