Hide certain values in output from JSON.stringify()

后端 未结 13 1581
有刺的猬
有刺的猬 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:54

    You can use native function defineProperty from Object:

    var data = {a: 10};
    Object.defineProperty(data, 'transient', {value: 'static', writable: true});
    data.transient = 'dasda';
    console.log(JSON.stringify(data)); //{"a":10}
    

提交回复
热议问题