# Adding 0 to a balance on an object

How can I refactor this into one line? Is that possible, even with ES6 syntax?

export const zeroBalance = (coin) => {
coin.balance = 0;
return coin;
};

• Do you need to mutate the object? – xehpuk Jul 3 '17 at 9:24
• @xehpuk yes, though I guess I could make a new object from both my local search coins object and the remote api coins. In my case above, I use the local search modal just for the search, once a full coin comes back from the api, there is no balance key for my UI, so I add it. – Leon Gaban Jul 5 '17 at 12:19
• uh.. remove the linebreaks..? – I wrestled a bear once. Jul 5 '17 at 15:11

How can I refactor this into one line? Is that possible, even with ES6 syntax?

export const zeroBalance = (coin) => {
coin.balance = 0;
return coin;
};


Yes, that's easily possible. Newlines aren't required in ECMAScript:

export const zeroBalance = coin => { coin.balance = 0; return coin; };


Ruby has an implementation of the K Combinator as Object#tap for exactly that use case. ECMAScript doesn't, but it's easy to implement:

 function k(obj, f) { f(obj); return obj };


This would allow you to rewrite your code like this:

export const zeroBalance = coin => k(coin, c => c.balance = 0);

• Yeah I guess this is the right answer, I asked the original question incorrectly. This is the minified view, was hoping there would be a clean sexy way to do it, but thanks :) – Leon Gaban Jul 2 '17 at 23:31
• See the update. – Jörg W Mittag Jul 2 '17 at 23:52

You can use the comma operator, which takes the value of the last expression. It's not used very much, so may make the code less readable, but if you wanna make it one line, this is the way to do it in a single statement. You need parentheses because of operator presedence. If you omitted them, the comma would be interpreted as a separator between const variable definitions.

export const zeroBalance = (coin) => (coin.balance = 0, coin);

• Uh, nice, I totally forgot about that. – Jörg W Mittag Jul 3 '17 at 10:40

As it's currently written, this function creates a pitfall: it modifies supplied object and returns the result. I suggest to modify it to be a pure function (it can be a one-liner):

export const zeroBalance = coin => Object.assign({}, coin, { balance: 0 });

• You can alternatively use the spread operator: coin => ({ ...coin, balance: 0 }). Adding an example of how the function would be used could be useful (updatedCoin = zeroBalance(coin) vs zeroBalance(coin); coin // now has zero balance) and some explanation about why a pure function might help (debugging, testing, etc) – Craig Ayre Jul 3 '17 at 9:55
• @CraigAyre please don't push unfinilazed ES proposals, OP specifically said 'in ES6'; agree with the rest of your comment, though. – Pavlo Jul 4 '17 at 10:16

You could make balance be a function on the coin that modifies the value and return this, then you could do coin.balance(0). (Then again, you just move the two lines from one place to another).

Other than that, there it is not possible to my knowledge. You can't change a property on the object and return the object at the same line.

Don't.

There is no advantage to making this function single line. You have two distinct operations, and keeping them separate makes the function much clearer. You gain nothing by making it single line, and you lose readability.