Conditional object properties

Reinis Ivanovs dabas at untu.ms
Wed Nov 2 21:59:59 UTC 2016


It works in Babel without the parenthesis.

On Wed, Nov 2, 2016 at 5:08 PM, Luke Mitchell <luke at enki.com> wrote:

> That works, although you have to add parenthesis. It's a nice solution
> but it is still a bit more verbose with the spread, the parenthesis
> and the `: null`.
>
> ```
> let cond = true;
> let obj = {
>   prop1: 'hello',
>   ...(cond ? {prop2: 'world'} : null)};
> // { prop1: 'hello', prop2: 'world' }
>
> cond = false;
> let obj = {
>   prop1: 'hello',
>   ...(cond ? {prop2: 'world'} : null)};
> // { prop1: 'hello' }
> ```
>
> On Wed, Nov 2, 2016 at 2:42 PM, Reinis Ivanovs <dabas at untu.ms> wrote:
> > No need for new syntax, you can just do this:
> >
> > ```
> > {...cond ? {prop: value} : null}
> > ```
> >
> > I find it about the same in readability.
> >
> > On Wed, Nov 2, 2016 at 2:18 PM, Luke Mitchell <luke at enki.com> wrote:
> >>
> >> Hi all,
> >>
> >> I often come across a situation where I wish to extend an object
> >> (usually an argument for an API request) if a particular condition is
> >> satisfied, such as the presence of a function parameter. Currently the
> >> only way to do this is by assigning the object to a variable, checking
> >> the condition using an `if` statement and then extending the object. I
> >> am proposing the inclusion of an operator that allows a property to be
> >> included, subject to a particular condition, inside the object
> >> definition.
> >>
> >> The operator looks like this:
> >>
> >> ```
> >> let obj = {
> >>   cond ? prop: value
> >> };
> >>
> >> ```
> >>
> >> The operator could also be used with a block statement, allowing
> >> multiple properties to be included:
> >>
> >> ```
> >> let obj = {
> >>   cond ? {
> >>     prop1: value1,
> >>     prop2: value2
> >>   }
> >> };
> >> ```
> >>
> >> I have created a draft proposal and would love to hear any thoughts
> >> you have on the matter. You can find it on my GitHub, at the link
> >> below.
> >>
> >> https://github.com/lukem512/proposal-condition-property-definitions
> >>
> >> Kind regards,
> >> Luke
> >> _______________________________________________
> >> es-discuss mailing list
> >> es-discuss at mozilla.org
> >> https://mail.mozilla.org/listinfo/es-discuss
> >
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20161102/b6c535b4/attachment.html>


More information about the es-discuss mailing list