Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js根据其值返回对象键_Javascript_Node.js - Fatal编程技术网

Javascript Node.js根据其值返回对象键

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 } } 我

在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 }
}
我们可以根据
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
,第二个仍然是两行答案。这不是我想要的。谢谢