Little-known uses of JSON.stringify
There are many common functions in JS. We may use them every day, but we don't know some of their extra functions. JSON.stringify
is such a function. Let’s take a look at its special usage today.
Basics
##The JSON.stringify method receives a variable and It is converted into JSON representation.
const boy = { name: 'John', age: 23 }; JSON.stringify(boy); // {"name":"John","age":23}
const boy = { name: 'John', age: 23, hobbies: new Map([[0, 'coding'], [1, 'earn money']]) } JSON.stringify(boy) // {"name":"John","age":23,"hobbies":{}}
The second parameter
JSON.stringify can receive the second parameter, which can be called
replacer Replacer.
const boy = { name: 'John', age: 23 } JSON.stringify(boy, ['name']) // {"name":"John"}
replacer parameter can also receive a function. This function iterates through the entire object, passing in the keys and values, and lets you decide how to replace them.
const boy = { name: 'John', age: 23, hobbies: new Map([[0, 'coding'], [1, 'earn money']]) } JSON.stringify(boy, (key, value) => { if (value instanceof Map) { return [...value.values()] } return value }) // {"name":"John","age":23,"hobbies":["coding","earn money"]}
undefined (returning
null is not acceptable), this attribute will be removed:
JSON.stringify(boy, (key, value) => { if (typeof value === 'string') { return undefined } return value }) // {"age":23,"hobbies":{}}
The third parameter
The third parameterspace controls the spacing of the converted JSON string.
JSON.stringify(boy, null, 2) // { // "name": "John", // "age": 23, // "hobbies": {} // }
JSON.stringify(boy, null, '--') // { // --"name": "John", // --"age": 23, // --"hobbies": {} // }
toJSON method
If the object we want to convert has atoJSON method, then we can customize the process of being serialized. . Instead of serializing the object, you can return a new value from the method, and this value will be serialized instead of the original object.
const boy = { name: 'John', age: 23, hobbies: new Map([[0, 'coding'], [1, 'earn money']]), toJSON() { return { name: `${this.name} (${this.age})`, favorite: this.hobbies.get(0) } } } JSON.stringify(boy) // {"name":"John (23)","favorite":"coding"}
Reference article
- Original address: https:// mp.weixin.qq.com/s/_e83_G7RjVt2eR_ro7blOA
- MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON /stringify
js tutorial column, welcome to learn!
The above is the detailed content of Little-known uses of JSON.stringify. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.
