I often run into situations where I need to construct an object in JavaScript, and I want to set fields to this object conditionally. That could be an options object that I am passing as a parameter to some other function or API. I have only some of the field values available, and only those should be part of the parameter object.

To describe the situation, let’s take a look at an example with a straightforward syntax using if statements.

#programming #coding #es6 #javascript #tips

How to add object fields conditionally in JavaScript
1.20 GEEK