最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can Javascript spread operator include undefined fields of an object? - Stack Overflow

programmeradmin1浏览0评论

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)

Share Improve this question edited Jan 27, 2019 at 3:19 dwjohnston 12k39 gold badges117 silver badges218 bronze badges asked Jan 16, 2019 at 16:13 iGoodieiGoodie 1621 silver badge10 bronze badges 6
  • 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 field f2 with any value, including undefined, it will be copied to B 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 using Response::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
 |  Show 1 more ment

2 Answers 2

Reset to default 3

If 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

发布评论

评论列表(0)

  1. 暂无评论