Question

I have a reduce function like this:

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

which works fine, but now the name of the second argument compiles to _arg. How can I give it a different name? I've tried several different approaches like arg = { commit: id: cid, type: type } and { commit: id: cid, type: type } : arg and { commit: id: cid, type: type } = arg but nothing compiles to the intended result. What's wrong with my syntax?

Was it helpful?

Solution

Why do you care what the second argument is called? Your object destructuring means that you wouldn't work with that argument at all, you'd just work with cid and type instead. The _arg name and even its existence is subject to change and none of your business.

For example, if you have this:

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
, { }

then you'll get 1, 2 and 2, 3 in the console. If you want the whole second argument then give it a name and unpack it inside the iterator function:

ops = rqOps.reduce (p, arg, idx, arr) ->
    { commit: id: cid, type: type } = arg
    #...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top