Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the unusual uses of JSON stringify ()

Shulou Source: shulou.com Published: 2022-06-02 15:49:53 10月03日 Update

This article mainly explains "what are the unusual uses of JSON stringify ()". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the unusual uses of JSON stringify ().

JSON.stringify () serializes only traverable properties (enumerable=true)

Copy the code

Varobj= {}

Object.defineProperties (obj, {

'foo': {

Value:1

Enumerable:true

}

'bar': {

Value:2

Enumerable:false

}

})

JSON.stringify (obj); / / "{" foo ": 1}"

Copy the code

JSON.stringify (obj,fn | arr)

1) when the second parameter is an array, it represents the list of attributes of the object that needs to be serialized

2) when the second parameter is a method, function (key,value) {} represents the key-value value corresponding to each attribute, and the final return value can be used to change the original result.

This method can be used to change the contents of an object before serialization.

Copy the code

Varo= {a: {b:1}}

Functionf (key,value) {

Console.log ("[" + key+ "]:" + value)

Returnvalue

}

JSON.stringify (ofline f)

/ / []: [objectObject]

/ / [a]: [objectObject]

/ / [b]: 1

/ /'{"a": {"b": 1}}'

/ / Recursive processing, the last returned result is processed each time

Copy the code

JSON.stringify (obj,null,number | string)

You can accept the third parameter, which can be used to print objects in format

When the third parameter is a numeric value, it represents the number of spaces before each attribute

Tags: Code common parameters attributes content objects sequences learning three methods results processing that is ideas situations numerical values arrays articles more formats Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info OPPO Reno Huawei Xiaomi