Hide certain values in output from JSON.stringify()

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

    you can do it easily with ES2017

    let {privateProperty1:exc1, privateProperty2:exc2, ...foo} = {
        x:0,
        y:0,
        divID:"xyz",
        privateProperty1: 'foo',
        privateProperty2: 'bar'
    }
    

    Here privateProperty1 and privateProperty2 are assigned to exc1 and exc2 accordingly. The remainings are assigned to foo newly created variable

提交回复
热议问题