A way to prevent properties to be added to an object if they are null or undefined.

Bob Myers rtm at gol.com
Wed Nov 29 02:56:52 UTC 2017


The usual idiom is

```
Object.assign({}, couldBeNull && {couldBeNull})
```

taking advantage of the fact that `Object.assign` skips over non-object
parameters.

I don't understand your proposed

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

and it seems to be syntactically wrong, but if you want to use object
spread syntax you could write

```
{...couldBeNull && {couldBeNull}}
```







On Wed, Nov 29, 2017 at 7:37 AM, J Decker <d3ck0r at gmail.com> wrote:

> 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 <michalwadas at gmail.com>
> wrote:
>
>> You can just use proxy with proper set trap.
>>
>> On Wed, 29 Nov 2017 at 02:30, Rodrigo Carranza <
>> rodrigocarranza at 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://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>
>>> <#m_1553661519197345990_m_1540481831920124741_m_-273853769969316494_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>>> _______________________________________________
>>> es-discuss mailing list
>>> es-discuss at mozilla.org
>>> https://mail.mozilla.org/listinfo/es-discuss
>>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
>
> _______________________________________________
> 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/20171129/34227e73/attachment-0001.html>


More information about the es-discuss mailing list