JSON.stringify接受另外2个参数

Do you know that JSON.stringify can actually take 2 other parameters? I didn't know this before I laid my eyes on my colleague's pull request. (I'm such a noob) But it's nothing difficult. Those 2 parameters will help optimize the result in a good format.

您知道JSON.stringify实际上可以接受其他2个参数吗? 在把目光投向同事的请求之前,我还不知道这一点。 (我真是个菜鸟)但这并不困难。 这两个参数将有助于以良好的格式优化结果。

In my opinion, the usage of JSON.stringify parameters is never a rare case. Well, let's take a look at those "I-didn't-know" features that "educated" me.

在我看来,使用JSON.stringify参数绝不是罕见的情况。 好吧,让我们看一下那些“教育”我的“我不知道”的功能。

Visit https://pitayan.com/posts/json-stringify-params/ to read the original article. We also have beautiful source code highlights.

访问https://pitayan.com/posts/json-stringify-params/以阅读原始文章。 我们还提供了漂亮的源代码摘要。

1.替换程序:过滤属性 (1. Replacer: Filtering your properties)

This parameter is of course optional by default. By assigning Array of Number or String, the output JSON will return the stringified properties given in the Array.

默认情况下,此参数当然是optional的。 通过分配ArrayNumberString ,输出JSON将返回给定的字符串化的特性Array

const obj = {
"site": "Pitayan",
"url": "https://pitayan.com",
100: 100
}JSON.stringify(obj, ['site', 100])
// "{\"site\":\"Pitayan\",\"100\":100}"

This is extremely helpful when I only some of the properties inside the Object.

当我仅使用Object内部的某些属性时,这将非常有用。

But it comes to negations, the 2nd parameter will not provide any help. Well, I suppose this is how this API is designed initially. In such a case, it’s better to handle the Object properties before JSON.stringify.

但涉及到否定,第二个参数不会提供任何帮助。 好吧,我想这就是最初设计此API的方式。 在这种情况下,最好在JSON.stringify之前处理Object属性。

2.空格:格式化字符串JSON (2. Space: Formatting the string JSON)

JSON.stringify offers another useful parameter that allows users to format the string output with whitespaces.

JSON.stringify提供了另一个有用的参数,该参数允许用户使用空格格式化字符串输出。

Frankly speaking, I don’t quite need it, since I could print out the Object in the browser console directly. But it is truly helpful when the JSON data is big enough, and I’m printing out data in the terminal.

坦白地说,我并不需要它,因为我可以直接在浏览器控制台中打印出对象。 但是,当JSON数据足够大且我正在终端中打印数据时,这确实有用。

// Without formatter
JSON.stringify(obj)
// "{\"site\":\"Pitayan\","url\":\"https://pitayan.com\",\"100\":100}"

This looks prettier, isn’t it?

这看起来更漂亮,不是吗?

// With formatter
JSON.stringify(obj, null, 2)
// "{
// \"100\": 100,
// \"site\": \"Pitayan\",
// \"url\": \"https://pitayan.com\"
// }"

3. toJSON (3. toJSON)

After I realized that there are 2 parameters for JSON.stringify, I decided to take a look at the official document. Then I found that an Object can define a method to control the behavior of JSON.stringify.

在意识到JSON.stringify有2个参数JSON.stringify ,我决定看一下官方文档。 然后我发现一个Object可以定义一个方法来控制JSON.stringify的行为。

It’s intercepting the stringify process and a proper String value must be returned from toJSON method. Otherwise, the output is undefined.

它拦截了stringify进程,并且必须从toJSON方法返回正确的String值。 否则,输出是undefined

toJSON receives an argument which is the key of the target Object if it's nested within another one.

toJSON接收到一个参数,如果它嵌套在另一个Object ,则该参数是目标Object的键。

const objToJSON = {
"site": "pitayan",
"url": "https://pitayan.com",
toJSON: function (key) {
if (key) {
return `[${key}]: ${this.site} -- ${this.url}`
} else {
return `${this.site} -- ${this.url}`
}
}
}
JSON.stringify(objToJSON)
// "\"pitayan -- https://pitayan.com\""JSON.stringify({ objToJSON })
// "{\"objToJSON\":\"[objToJSON]: pitayan -- https://pitayan.com\"}"

Okay, this is all for JSON.stringify. Hope this article will help everyone gain some knowledge about this useful API.

好的,这就是JSON.stringify全部JSON.stringify 。 希望本文能帮助所有人获得有关此有用API的一些知识。

If you think this article is great, please do share it on the social network. Thanks for reading!

如果您认为本文很棒,请在社交网络上分享。 谢谢阅读!

翻译自: https://medium.com/weekly-webtips/json-stringify-accepts-2-other-parameters-17232e5d2594

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值