________________________________

Yeah, it was wrong, I'm currently using like

```js
let ret = { ... ( couldBeNull ? {couldBeNull} : {} ) }
```

Yours is better and the usual idiom a bit hard to understand.

What I'm proposing is something like this

```js
let ret = { couldBeNull? }
```
or if you want a different name for the property

```js
let ret = { bar ?: couldBeNull }
```

It is not like there is no way to do this, there are plenty. But this way could 
make code a bit easier to read.

P.D: Sorry for the duplicates and that new thread, I don't understand so much 
this client and it is acting weird. Also I'm new to mailing lists.

________________________________
De: J Decker <d3c...@gmail.com>
Enviado: martes, 28 de noviembre de 2017 09:07 p.m.
Para: Michał Wadas
Cc: Rodrigo Carranza; es-discuss@mozilla.org
Asunto: Re: A way to prevent properties to be added to an object if they are 
null or undefined.

Or feed it through JSON.parse( JSON.strinigfy( o ) ) which will delete 
undefined things; doesn't help with null.

On Tue, Nov 28, 2017 at 5:50 PM, Michał Wadas 
<michalwa...@gmail.com<mailto:michalwa...@gmail.com>> wrote:
You can just use proxy with proper set trap.

On Wed, 29 Nov 2017 at 02:30, Rodrigo Carranza 
<rodrigocarra...@outlook.com<mailto:rodrigocarra...@outlook.com>> wrote:
A way to prevent properties to be added to an object if they are null or 
undefined.

Currently this can be accomplished in many ways:

With an if:
```js
function foo(couldBeNull){
let ret = {}
if(couldBeNull){
ret.couldBeNull = couldBeNull
}
return  ret
}
```

With ternary (kind of gross)
```js
function foo(couldBeNull){
let ret = {}
couldBeNull ? (ret.couldBeNull = couldBeNull) : null
return  ret
}
```

Also gross
```js
function foo(couldBeNull){
let ret = {}
couldBeNull && (ret.couldBeNull = couldBeNull)
return  ret
}
```

A bit hard to read:
```js
function foo(couldBeNull){
let ret = {
...({couldBeNull} : {})
}
return  ret
}
```

Requires importing a lib or writing the function by yourself. Also it has to 
iterate over all values
```js
function foo(couldBeNull){
let ret = {
couldBeNull
}
ret = removeEmptyValues(ret) // imported from some library
return  ret
}
```

Wouldn't it be better something like this?:

```js
function foo(couldBeNull){
let ret = {
couldBeNull?
}
return  ret
}
```

Or if you want to set other property name

```js
function foo(couldBeNull){
let ret = {
bar ?:  couldBeNull // bar is not added if couldBeNull is null or undefined
}
return  ret
}
```

[https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif]<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
      Libre de virus. 
www.avast.com<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org<mailto:es-discuss@mozilla.org>
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org<mailto:es-discuss@mozilla.org>
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to