Rodrigo Carranza (2017-11-29T06:15:29.000Z)
rodrigocarranza at outlook.com (2017-11-29T06:41:01.706Z)
I know how to do it, also, what about nested objects. It is not about serialization, and falsy values should be explicitly compared to their own empty value. ```js const propertyDict = { aa ?: null, bb ?: undefined, cc ?: couldBeZero !== 0 ? couldBeZero : null, dd ?: couldBeFalse !== False ? couldBeFalse : null, ee ?: couldBeEmptyString !== "" ? couldBeEmptyString : null, } ``` of course you could use the falsy values with `or` ```js const propertyDict = { aa ?: null, bb ?: undefined, cc ?: couldBeZero || null, //doesn't added if couldBeZero is null or falsy(0, "") dd ?: couldBeFalse || null, ee ?: couldBeEmptyString || null, } ``` if you don't intend to remove falsy values from the object but still don't want null properties ```js const propertyDict= { aa ?: null, bb ?: undefined, cc ?: couldBeZeroOrNull, dd ?: couldBeFalseOrNull, ee ?: couldBeEmptyStringOrNull, } ``` why is for me a concern currently(I always had this need, but I will show a current use case). Suppose you want to filter from a collection in mongodb you will use Collection#find method, you pass an object as your first parameters to filter out the collection. ```js function filterPosts({ categoryId, postType }){ // if non of them is present(==null) I want to get all the Posts // if either of them is present I want to use them as the filter // currently I have to go like this const query = { ...(categoryId? {categoryId} : {}), ...(postType? {postType} : {}) } /* but normally I could forget and just do this const query = { categoryId, postType, } If you pass null in one of the properties, the database will perform an equality comparison to null and will return nothing if both are null. That's why I need to remove properties with null values before sending the query. */ return db.collection('Posts').find(query) } ``` is better to have a null check there ```js function filterPosts({categoryId, postType}){ return db.collection('Posts').find({ categoryId?, postType? }) } ```