JavaScript ã®ä¾å¤ "cyclic object value" ã¯ã JSON ã®ä¸ã«ãªãã¸ã§ã¯ãã®åç
§ãè¦ã¤ãã£ãã¨ãã«çºçãã¾ãã JSON.stringify()
ã¯ããã解決ãããã¨ãããããã«ãã£ã¦å¤±æãã¾ãã
TypeError: cyclic object value (Firefox) TypeError: Converting circular structure to JSON (Chrome and Opera) TypeError: Circular reference in value argument not supported (Edge)ã¨ã©ã¼ã®ç¨®é¡ ã¨ã©ã¼ã®åå
JSON å½¢å¼ã¯ãªãã¸ã§ã¯ãåç
§ã«å¯¾å¿ãã¦ãã¾ãã (IETF ã®èæ¡ã¯ããã¾ãã)ããããã£ã¦ JSON.stringify()
ã¯ããã解決ãããã¨ãããããã«ãã£ã¦å¤±æãã¾ãã
次ã®ãããªå¾ªç°æ§é ä½ã§ã¯ã
var circularReference = { otherData: 123 };
circularReference.myself = circularReference;
JSON.stringify()
ã¯å¤±æãã¾ãã
JSON.stringify(circularReference);
// TypeError: cyclic object value
循ç°åç §ãã·ãªã¢ã©ã¤ãºããã«ã¯ãããã«å¯¾å¿ããã©ã¤ãã©ãª (ä¾ãã° cycle.js) ã使ç¨ããããèªåèªèº«ã§å¾ªç°åç §ãæ¢ãã¦ã·ãªã¢ã©ã¤ãºå¯è½ãªå¤ã«ç½®ãæãã (ã¾ãã¯åé¤ãã) ãã¨ãæ±ãã解決çãå®è£ ãããã¨ãã§ãã¾ãã
次ã®ã¹ããããã¯ã JSON.stringify()
ã® replacer
弿°ã使ç¨ãã¦å¾ªç°åç
§ãæ¤ç´¢ãã¦ãã£ã«ã¿ãªã³ã°ããæ¹æ³ã示ãã¦ãã¾ã (ããã«ãããã¼ã¿æå¤±ãçºçãã¾ã)ã
const getCircularReplacer = () => {
const seen = new WeakSet();
return (key, value) => {
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
}
return value;
};
};
JSON.stringify(circularReference, getCircularReplacer());
// {"otherData":123}
é¢é£æ
å ±
JSON.stringify
JSON.decycle
㨠JSON.retrocycle
ã¨ãã 2 ã¤ã®é¢æ°ãå°å
¥ããå¾ªç°æ§é 㨠dag ã JSON ã§ã¨ã³ã³ã¼ããã¦ãããªã«ããªã¼ã§ãã¾ããRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4