TIL — The power of JSON.stringify replacer parameter | pawelgrzybek.com

JSON.stringify 는 열거 가능한 자기 속성만 본다 — Set·Map 은 {} 가 되니 replacer 로 바꿔 넣는다. space 문자열은 10자에서 잘린다.1

JSON.stringify(dude, (key, value) =>
  value instanceof Set ? [...value] : value
);
JSON.stringify(dude, null, "🍆");

Footnotes

  1. JSON.stringify() - MDN: “Only enumerable own properties are visited. This means Map, Set, etc. will become “{}”. You can use the replacer parameter to serialize them to something more useful.“ / “Number values of space are clamped to 10, and string values are truncated to 10 characters.” ↩

`#124`