• Using json. stringify to Work with JavaScript objects
  • Author: TRAN SON HOANG
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Inchill
  • Proofreader: Chorer, z0gSh1u

Use json.stringify to process JavaScript objects

In this article, I’ll share how to use the second argument to json.stringify (), replacer.

Review the grammar

I’m sure most of you know the first argument to json.stringify (). However, only some people know that there is a second parameter called replacer. So I want to reintroduce the syntax of this method.

The json.stringify () method will do two things:

  1. Convert the value to a JSON string.
  2. The value continues to be converted if a replacer conversion function is specified.
JSON.stringify(value[, replacer[, space]])
Copy the code

There are three parameters:

  1. value

The value to be converted to a JSON string. 2. Replacer changes the custom handler for the string serialization process. 3. Space Adds a blank string for indentation in output.

Value supports multiple data types, such as objects, arrays, and primitive data types.

Some values return undefined, such as the function, Symbol type, and undefined.

So what do Object instances such as Map, Set, WeakMap, and WeakSet return?

Note:

  • Infinity,NaNnullWill be regarded asnull.
  • After encoding, object attribute names and strings are enclosed in double quotes.

Use the Replacer parameter

The object is recursively serialized into a JSON string, while the replacer function is called on each property. This is a great help when working with objects in JavaScript.

There are two types of replacers:

  • An array of
  • function

Replacer as an array

When you need to filter object properties, you can apply replacer as an array.

Only those properties that are passed to the array are encoded.

Replacer as a function

Replacer can be used as a function if we do not know the exact property names, or if there are too many properties to list them all.

Therefore, we write a function to filter attribute values that conform to a certain data type or a particular schema.

Replacer is useful for simple cases where we want to retrieve some properties from an object.

We sometimes maintain old projects without any good tools to filter properties from large JSON objects.

I hope this article has been helpful! Feel free to leave any questions you have in the comments below. I’m happy to help!

reference

[1] Json Stringify: developer.mozilla.org/en-US/docs/…

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.