Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON.stringify #9

Open
xiaokeqi opened this issue Jul 25, 2019 · 1 comment
Open

JSON.stringify #9

xiaokeqi opened this issue Jul 25, 2019 · 1 comment

Comments

@xiaokeqi
Copy link
Owner

今天看木易杨的每日一题(113),要求写一个去重函数
第一想法,是通过递归函数实现
看到大神们的回复后
发现,可通过JSON.stringify实现。
原理呢是因为,JSON.stringify可以把对象,序列化为json字符串。
字符串相等的。则为重复的。
通过set集合,即可将重复的,去除掉。
链接:Advanced-Frontend/Daily-Interview-Question#215

@xiaokeqi
Copy link
Owner Author

xiaokeqi commented Jul 31, 2019

JSON.stringify(value[, replacer [, space]])

参数

value
将要序列化成 一个JSON 字符串的值。
replacer 可选

如果该参数是一个函数,则在序列化过程中,被序列化的值的每个属性都会经过该函数的转换和处理;如果该参数是一个数组,则只有包含在这个数组中的属性名才会被序列化到最终的 JSON 字符串中;如果该参数为null或者未提供,则对象所有的属性都会被序列化;关于该参数更详细的解释和示例,请参考使用原生的 JSON 对象一文。

space 可选
指定缩进用的空白字符串,用于美化输出(pretty-print);如果参数是个数字,它代表有多少的空格;上限为10。该值若小于1,则意味着没有空格;如果该参数为字符串(字符串的前十个字母),该字符串将被作为空格;如果该参数没有提供(或者为null)将没有空格。

function replacer(key, value) {
  if (typeof value === "string") {
    return undefined;
  }
  return value;
}

var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
var jsonString = JSON.stringify(foo, replacer);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant