front-end-interview-questions icon indicating copy to clipboard operation
front-end-interview-questions copied to clipboard

【手写实现题目】JSON.stringify

Open yayxs opened this issue 4 years ago • 1 comments

yayxs avatar Oct 31 '20 05:10 yayxs

let student = {
  name: 'John',
  age: 30,
  isAdmin: false,
  courses: ['html', 'css', 'js'],
  wife: null,
  sayHi() {
    // 被忽略
    alert('Hello');
  },
  [Symbol('id')]: 123, // 被忽略
  something: undefined, // 被忽略
};

let str = JSON.stringify(student);

console.log(str);

完整的语法 let json = JSON.stringify(value[, replacer, space])

let user = {
  name: "John",
  age: 25,
  roles: {
    isAdmin: false,
    isEditor: true
  }
};

alert(JSON.stringify(user, null, 2));
/* 两个空格的缩进:
{
  "name": "John",
  "age": 25,
  "roles": {
    "isAdmin": false,
    "isEditor": true
  }
}
*/

/* 对于 JSON.stringify(user, null, 4) 的结果会有更多缩进:
{
    "name": "John",
    "age": 25,
    "roles": {
        "isAdmin": false,
        "isEditor": true
    }
}
*/

yayxs avatar Dec 26 '20 03:12 yayxs