0

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?

zakdances
  • 22,285
  • 32
  • 102
  • 173

1 Answers1

2

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
    #...
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Your last block does replace `_arg` with `arg`. There's no other difference in the compiled code. – hpaulj Dec 10 '13 at 19:26
  • @hpaulj: Sorry, misread your comment the first time, my eyes saw "doesn't" instead of "does". – mu is too short Dec 10 '13 at 19:46
  • I want to reference the second argument because I only want to destructure a few of its properties. What if the object has 100 properties and I only want to destructure 2 of them while still having access to the others? – zakdances Dec 10 '13 at 22:54
  • Then use the second approach: give it a name in the argument list and pull out the parts you want inside the function body. – mu is too short Dec 10 '13 at 22:56