Hide certain values in output from JSON.stringify()

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

    Easier way to do.

    1. Create a variable and assign an empty array. This makes object to be the prototype of array.
    2. Add non numeric keys on this object.
    3. Serialize this object using JSON.stringify
    4. You will see that nothing is serialized from this object.

    ~~~

    var myobject={
      a:10,
      b:[]
    };
    
    myobject.b.hidden1 = 'hiddenValue1';
    myobject.b.hidden2 = 'hiddenValue2';
    
    //output of stringify 
    //{
    //    "a": 10,
    //    "b": []
    //}
    

    ~~~

    http://www.markandey.com/2015/07/how-to-hide-few-keys-from-being-being.html

提交回复
热议问题