简体   繁体   English

Coffeescript数组理解跳过值

[英]Coffeescript array comprehension skip value

I have an RPC library that I am porting to coffeescript. 我有一个要移植到coffeescript的RPC库。 One of the things that it has to do is reorder function call parameters to make sure that they are in the correct order. 它要做的事情之一是对函数调用参数进行重新排序,以确保它们的顺序正确。 To do this I have written an "array comprehension" that looks like this: 为此,我编写了一个“数组理解”,如下所示:

argValues = for param in paramNames
                if param of args
                    args[param]
                else if param isnt 'cb'
                    throw new Error "Missing argument for paramater '#{param}' of procedure '#{func}'"
        argValues[-1..-1] = cb

The 'cb' param is taken by all the remote procedures to provide results via a callback. 所有远程过程都采用“ cb”参数,以通过回调提供结果。 This needs to be skipped by the comprehension because the client doesn't provide this callback (the server does so that the results can be encoded and written for return to the client). 由于客户端不提供此回调(服务器提供了回调,因此可以对结果进行编码和写入以返回给客户端),因此,理解应该跳过此步骤。 My issue is that the comprehension is putting in the value 'undefined' for this, so I have to replace the undefined with my callback using the clunky [-1..-1] syntax. 我的问题是理解力为此输入了'undefined'值,因此我必须使用笨拙的[-1..-1]语法用我的回调替换undefined。 What I would rather do is skip over it and call argValues.push cb . 我更愿意做的是跳过它,并调用argValues.push cb

Is there a way to have a comprehension skip a value like this? 有没有办法让理解力跳过这样的值?

There is a when clause you can use with loops but the fine manual only includes it in some examples. 有一个when子句可以与循环一起使用,但是精美的手册仅在某些示例中包括了它。 when allows you to apply conditions to the loop variables before the loop body is executed. when允许您在执行循环主体之前将条件应用于循环变量。

If you want to skip param s that aren't in args then 如果要跳过不在args param ,则

for param in paramNames when param !of args
    args[param]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM