In the project I'm working on, I find myself dealing with a lot of parsing and validating tasks. This often results in 5-10+ lines of code like if(value) object.value = value
.
I considered using
object.value = value || (your favorite falsy value)
approach, but then the property ends up with a falsy value.
Here's an example (not actual production code):
let filter = {}
let thing = ctx.query.thing
thing = Validation.validateThingy(thing)
if(thing) filter.thing = thing
// +50 more parameter parsing/request body parsing
return DB.find(filter).then(etc...)
Is there a more elegant solution to handle this situation without resorting to ||
or iterating over the object's properties to filter out falsy values?