Hide certain values in output from JSON.stringify()

后端 未结 13 1514
有刺的猬
有刺的猬 2020-12-02 15:13

Is it possible to exclude certain fields from being included in the json string?

Here is some pseudo code

var x = {
    x:0,
    y:0,
    divID:\"xyz         


        
相关标签:
13条回答
  • 2020-12-02 15:56

    Here is my approach with the spread operator (...) :

    const obj = { name:"hello", age:42, id:"3942" };
    const objWithoutId = { ...o, id: undefined }
    
    const jsonWithoutId = JSON.stringify({...o, id:undefined});
    
    0 讨论(0)
提交回复
热议问题