admin管理员组文章数量:1421951
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
本文标签: Can Javascript spread operator include undefined fields of an objectStack Overflow
版权声明:本文标题:Can Javascript spread operator include undefined fields of an object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745360859a2655285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论