https://qiita.com/qoAop/items/57d35a41ef9629351c3c
配列やプリミティブが指定できるのを知らなかった。
JSON.stringify({}); // '{}'
JSON.stringify(true); // 'true'
JSON.stringify("foo"); // '"foo"'
JSON.stringify([1, "false", false]); // '[1,"false",false]'
JSON.stringify([NaN, null, Infinity]); // '[null,null,null]'
JSON.stringify({ x: 5 }); // '{"x":5}'
第二引数にreplacerを指定できるのを知らなかった。 ネストしたオブジェクトも渡ってくるよう。 第一階層だけフィルターするのが難しいかも。
function replacer(key, value) {
// プロパティをフィルターする
if (typeof value === "string") {
return undefined;
}
return value;
}
var foo = {
foundation: "Mozilla",
model: "box",
week: 45,
transport: "car",
month: 7,
};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'