Is there a more readable way of spreading undefined fields of an object on another object without traversing every element of it?
Following example spreads object A
on object B
:
let A = { f1:'Foo', f2:'Bar', f3:'Baz' }
let B = { ...A }
// Now B has the value of { f1:'Foo', f2:'Bar', f3:'Baz' }
However in the following example spread operator will not include undefined values:
let A = { f1:'Foo', f2:undefined, f3:'Baz' }
let B = { ...A }
// Now B has the value of { f1:'Foo', f3:'Baz' }
// I would like it to be spread like { f1:'Foo', f2:undefined, f3:'Baz' }
// or { f1:'Foo', f2:null, f3:'Baz' }
Is there a way of projecting fields with undefined
value using spread operator? (and obviously WITHOUT traversing every field of the object A
and spreading into B
if the value of that field is not undefined
)
Is there a more readable way of spreading undefined fields of an object on another object without traversing every element of it?
Following example spreads object A
on object B
:
let A = { f1:'Foo', f2:'Bar', f3:'Baz' }
let B = { ...A }
// Now B has the value of { f1:'Foo', f2:'Bar', f3:'Baz' }
However in the following example spread operator will not include undefined values:
let A = { f1:'Foo', f2:undefined, f3:'Baz' }
let B = { ...A }
// Now B has the value of { f1:'Foo', f3:'Baz' }
// I would like it to be spread like { f1:'Foo', f2:undefined, f3:'Baz' }
// or { f1:'Foo', f2:null, f3:'Baz' }
Is there a way of projecting fields with undefined
value using spread operator? (and obviously WITHOUT traversing every field of the object A
and spreading into B
if the value of that field is not undefined
)
-
i can't reproduce problem.
let A = { f1:'Foo', f2:undefined, f3:'Baz' } let B = { ...A }
this is giving me expected result only. – Code Maniac Commented Jan 16, 2019 at 16:15 -
3
Your assertion is incorrect. If
A
has a fieldf2
with any value, includingundefined
, it will be copied toB
by the spread (which is not really an operator, for what that's worth). – Pointy Commented Jan 16, 2019 at 16:15 -
Oh that's right.. I guess it is
Express.js
excluding the fields with undefined upon usingResponse::json()
– iGoodie Commented Jan 16, 2019 at 16:18 -
1
@iGoodie that's because
undefined
isn't a legal value in JSON, despite it being a legal value of a key in a JS literal. – Alnitak Commented Jan 16, 2019 at 16:19 - 1 Note that while undefined is illegal, null is a valid value in JSON – Ferrybig Commented Jan 16, 2019 at 17:12
2 Answers
Reset to default 3If you're asking if the spread operator will maintain undefined property values 'post spread', they do.
const original = { one: 1, two: 2, three: undefined, four: null };
console.log(original);
const withSpread = {five: 5, ...original };
console.log(withSpread);
console.log(typeof withSpread.three === 'undefined')
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
It turned out to be an invalid assertion of mine. Spread operator indeed spreads fields with undefined
value. It was JSON.stringify()
removing those fields within one of my sources, which lead me to an invalid assertion.
For Express.js
users; you can use app.set('json replacer', (k, v) => v===undefined ? null : v);
to let express stringify your json response by replacing undefined
values with null
Or likewise, you can use JSON.stringify({...}, (k, v) => v===undefined ? null : v)
to let it stringify by replacing undefined
values with null