Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Coffeescript 我该如何重命名_arg";在解构函数参数中?_Coffeescript_Destructuring - Fatal编程技术网

Coffeescript 我该如何重命名_arg";在解构函数参数中?

Coffeescript 我该如何重命名_arg";在解构函数参数中?,coffeescript,destructuring,Coffeescript,Destructuring,我有这样一个reduce函数: ops = rqOps.reduce (p, { commit: id: cid, type: type }, idx, arr) -> # Do stuff here p , {} 这很好,但现在第二个参数的名称编译为\u arg。我怎么能给它起个不同的名字?我尝试了几种不同的方法,如arg={commit:id:cid,type:type}和{commit:id:cid,type:type}:arg和{commit:id:cid,typ

我有这样一个reduce函数:

ops = rqOps.reduce (p, { commit: id: cid, type: type }, idx, arr) ->
    # Do stuff here
    p
, {}

这很好,但现在第二个参数的名称编译为
\u arg
。我怎么能给它起个不同的名字?我尝试了几种不同的方法,如
arg={commit:id:cid,type:type}
{commit:id:cid,type:type}:arg
{commit:id:cid,type:type}=arg
,但没有编译到预期的结果。我的语法出了什么问题?

为什么您关心第二个参数的名称?您的对象解构意味着您根本不会使用该参数,而是使用
cid
type
\u arg
名称及其存在随时可能更改,与您无关

例如,如果您有:

rqOps = [
    { commit: { id: 1, type: 2 } }
    { commit: { id: 2, type: 4 } }
]
ops = rqOps.reduce (p, { commit: id: cid, type: type }, idx, arr) ->
    console.log(cid, type)
    p
, { }
然后您将在控制台中获得
1,2
2,3
。如果需要整个第二个参数,请给它一个名称,并将其解压缩到迭代器函数中:

ops = rqOps.reduce (p, arg, idx, arr) ->
    { commit: id: cid, type: type } = arg
    #...

您的最后一个块确实将
\u arg
替换为
arg
。编译后的代码没有其他区别。@hpaulj:对不起,第一次误读了您的注释,我的眼睛看到的是“不”而不是“做”。我想引用第二个参数,因为我只想对它的一些属性进行分解。如果对象有100个属性,而我只想在仍然可以访问其他属性的情况下分解其中的2个属性,那该怎么办?然后使用第二种方法:在参数列表中为其命名,并从函数体中取出所需的部分。