# Dynamic keys in object literals

**URL:** <https://es.discourse.group/t/dynamic-keys-in-object-literals/405>\
**Category:** 💡 Ideas\
**Created:** [July 22, 2020, 10:22pm UTC](https://es.discourse.group/t/dynamic-keys-in-object-literals/405 "2020-07-22T22:22:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gimenete](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/gimenete/32/2130_2.png) [@gimenete](https://es.discourse.group/u/gimenete)\
**Post date:** [July 22, 2020, 10:22pm UTC](https://es.discourse.group/t/dynamic-keys-in-object-literals/405/1 "2020-07-22T22:22:05Z")

</div>

👋 Hello. I have a small suggestion.

I find myself doing this many times:

```javascript
const ssl = !!process.env.SSL
const connectionOptions = {
  url: 'postgresql://...',
  ...ssl && { extra: { ssl: true } }
}

```

It works but feels a bit intimidating. I think there could be a better alternative:

```javascript
const ssl = !!process.env.SSL
const connectionOptions = {
  url: 'postgresql://...',
  extra: ssl ? { ssl: true }
}

```

It is similar to the ternary operator in purpose.

When `ssl` is true, the result would be:

`{ url: 'postgresql://...', extra: { ssl: true } }`

When `ssl` is false, the result would be:

`{ url: 'postgresql://...' }`

We could also allow a shorter version if the value we want is equal to the condition result:

```javascript
const cert = fs.writeFileSync(...)
const connectionOptions = {
  url: 'postgresql://...',
  ssl: cert?
}

```

When `ssl` is true (would work for any truthy value), the result would be:

`{ url: 'postgresql://...', ssl: <cert-content> }`

When `ssl` is a falsy value, the result would be:

`{ url: 'postgresql://...' }`

---

<div class="post-metadata">

**Author:** ![BenEmdon](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/benemdon/32/381_2.png) [@BenEmdon](https://es.discourse.group/u/BenEmdon)\
**Post date:** [July 22, 2020, 11:03pm UTC](https://es.discourse.group/t/dynamic-keys-in-object-literals/405/2 "2020-07-22T23:03:40Z")

</div>

@gimenete wouldn't this be a breaking change for apps that have objects using the key "extra"? Maybe this would need some special syntax?

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [July 23, 2020, 6:54am UTC](https://es.discourse.group/t/dynamic-keys-in-object-literals/405/3 "2020-07-23T06:54:13Z")

</div>

I'd recommend

```javascript
const connectionOptions = {
  url: 'postgresql://...',
  extra: ssl ? { ssl: true } : undefined,
}

```

It should be equivalent for all purposes.

---

<div class="post-metadata">

**Author:** ![gimenete](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/gimenete/32/2130_2.png) [@gimenete](https://es.discourse.group/u/gimenete)\
**Post date:** [July 23, 2020, 10:15am UTC](https://es.discourse.group/t/dynamic-keys-in-object-literals/405/4 "2020-07-23T10:15:52Z")

</div>

> [@BenEmdon](#):
>
> wouldn't this be a breaking change for apps that have objects using the key "extra"?

This is just an example. It could be `extra` or it could be anything. The syntax is

`key-name: condition ? value`

> [@bergus](#):
>
> It should be equivalent for all purposes.

There are circumstances where you don't want an `undefined` value and you want the key to not be there.
