欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

es6 json.stringify

ES6帶來了許多有用的功能,其中包括一個(gè)增強(qiáng)版的JSON.stringify()方法。以下是一些關(guān)于它的重要功能。

// 基本用法
const obj = { name: 'John', age: 30 };
const json = JSON.stringify(obj);
console.log(json); // {"name":"John","age":30"}
// 自定義過濾器
const obj2 = {
name: 'John',
age: 30,
favoriteBooks: ['1984', 'Animal Farm', 'Brave New World'],
password: 'topsecret'
};
const json2 = JSON.stringify(obj2, (key, value) =>{
if (key === 'password') {
return undefined;
}
return value;
});
console.log(json2); // {"name":"John","age":30,"favoriteBooks":["1984","Animal Farm","Brave New World"]}
// 美化輸出
const obj3 = {
name: 'John',
age: 30,
favoriteBooks: ['1984', 'Animal Farm', 'Brave New World']
};
const json3 = JSON.stringify(obj3, null, 2);
console.log(json3);
/*
{
"name": "John",
"age": 30,
"favoriteBooks": [
"1984",
"Animal Farm",
"Brave New World"
]
}
*/
// 替換屬性值
const obj4 = {
name: 'John',
age: 30,
favoriteBooks: ['1984', 'Animal Farm', 'Brave New World']
};
const json4 = JSON.stringify(obj4, (key, value) =>{
if (key === 'name') {
return 'Jane';
}
return value;
});
console.log(json4); // {"name":"Jane","age":30,"favoriteBooks":["1984","Animal Farm","Brave New World"]}

通過使用這些技巧,JSON.stringify()方法可以更靈活地處理JavaScript對(duì)象。它可以用于生成格式化的JSON,過濾不需要的屬性,替換屬性值等。