π Attention JavaScript Enthusiasts! π
Are you tired of writing lengthy code to conditionally add properties to JavaScript objects? π« I've got an e...
For further actions, you may consider blocking this person and/or reporting abuse
I actually don't find this easier to read.
At all.
Plus you'll end up with strange situations like where you're testing for a value which will exist but be null or 0.
Less code is better, but not at the sacrifice of legibility.
Perhaps you should consider the relatively new
??
operator?You have raised two concerns Readability and Validation:
Readability:
Initially when I got introduced to this, I also found it a bit tricky just like I found IIFE tricky especially with async/await but I guess we should evolve with respect to time and try to understand different cool ways to make code concise.
Validation
For this current context, if you get 0 then it won't pass in "if" check either, so proper validation and the concerns need to be taken place with respect to the context wherever we are applying our methods/techniques.
It's just a shortcut way of doing what I used to do in "if blocks" especially when making an API endpoint and you have to check and verify if any query parameter is available then add it as a filter to the DB.
Hey Shameel,
Thanks a ton for sharing this trick! π I've been coding in JavaScript for a while, but I never came across this elegant way to conditionally add properties to objects. Itβs going to make my code so much cleaner and more efficient. Can't wait to try this out in my next project. Keep these tips coming!
Thanks a lot for the appreciation :)
Probably a microoptimisation to take this into account, but the 'more readable' variant is 99.5% slower when the values are truthy and 95% slower when falsy according to jsbench.me (tested on a phone).
Other than that, if this is something you do often, just create a
addIfNotFalsy
method that does the if condition and use that.Not accounting for the helper method, this is (compared to the shortcut):
{...false}
is supposed to do according to the specs)This post did teach me that the
{...false}
situation is covered by the specs.I absolutely agree.
The code shown in the article is slow and unreadable.
Your solution is the best.
good idea
So as others have mentioned this is a bad idea because it's using falseyness to skip adding the property - that feels like it's really going to lead to some dodgy bugs with zeroes and "" etc. Surely the right way is to skip the property if it's not defined.
If your resultant object is going to be a property of a GraphQL request, or the object is going to be stringified then just setting the property to undefined will mean that it isn't actually persisted or used. It would however be present in an
in
query for the object. This isn't a problem for me so my code for this would read:I wouldn't be storing any extra data and the fact that this temporary version of the object has a couple of extra properties is a lot less memory and garbage usage than creating all of those temp objects.
Slow, too many spread operators involve for in operator internally which is unnecessary overhead.
For of, iterator
For of is only for iterable, spread operator for an object would use
for in
, object may not be iterable.Yes!
Spreading includes properties whose keys are symbols (which are ignored by Object.keys(), Object.values() and Object.entries()):
Absolutely agree.
"more readable and maintainable" rofl