Detailed explanation of how to solve the circular reference problem encountered when using JSON.stringify

Detailed explanation of how to solve the circular reference problem encountered when using JSON.stringify

When programmers do TypeScript/JavaScript development on a daily basis, they often need to serialize complex JavaScript objects into JSON strings through JSON.stringify and save them locally for subsequent specific analysis.

However, if the JavaScript object itself contains circular references, JSON.stringify does not work properly, with the error message:

VM415:1 Uncaught TypeError: Converting circular structure to JSON

The solution is to use the following code from this website to define a global cache array. Whenever the properties of the JavaScript object to be serialized are traversed, the value corresponding to the property is stored in the cache array.

If you find that an attribute value already exists in the cache array during the traversal, it means that a circular reference has been detected. In this case, you can simply return to exit the loop.

var cache = [];
var str = JSON.stringify(o, function(key, value) {
  if (typeof value === 'object' && value !== null) {
    if (cache.indexOf(value) !== -1) {
      // remove return;
    }
    // Collect all values ​​cache.push(value);
  }
  return value;
});
cache = null; // Clear the variable to facilitate garbage collection

Using this method, I successfully serialized a JavaScript object with a circular reference into a string.

This concludes this article on how to solve the circular reference problem encountered when using JSON.stringify. For more information about JSON.stringify circular references, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of how JS operates on pages inside and outside Iframe
  • How to make if judgment in js as smooth as silk
  • Implementation of a simplified version of JSON.stringify and its six major features explained in detail
  • Summary of various uses of JSON.stringify
  • Vue implements online preview of PDF files (using pdf.js/iframe/embed)
  • Summary of JavaScript JSON.stringify() usage
  • The difference and use of json.stringify() and json.parse()
  • Selenium+BeautifulSoup+json gets the json data in the Script tag
  • About if contains comma expression in JavaScript

<<:  Detailed tutorial for installing mysql5.7.18 on centos7.3

>>:  Linux kernel device driver character device driver notes

Recommend

Implementation of Vue3 style CSS variable injection

Table of contents summary Basic Example motivatio...

Detailed explanation of table return and index coverage examples in MySQL

Table of contents Index Type Index structure Nonc...

Implementation of proxy_pass in nginx reverse proxy

The format is simple: proxy_pass URL; The URL inc...

Analysis of the principles and usage of Docker container data volumes

What is a container data volume If the data is in...

Detailed explanation of VUE responsiveness principle

Table of contents 1. Responsive principle foundat...

The whole process of installing and configuring Harbor1.7 on CentOS7.5

1. Download the required packages wget -P /usr/lo...

Linux server SSH cracking prevention method (recommended)

1. The Linux server configures /etc/hosts.deny to...

Sample code for changing the color of a png image through a CSS3 filter

This method uses the drop-shadow filter in CSS3 t...

Detailed explanation of how to view the number of MySQL server threads

This article uses an example to describe how to v...

9 great JavaScript framework scripts for drawing charts on the web

9 great JavaScript framework scripts for drawing ...

Let's talk about the problem of Vue integrating sweetalert2 prompt component

Table of contents 1. Project Integration 1. CDN i...

MySQL index knowledge summary

The establishment of MySQL index is very importan...

CSS implements a pop-up window effect with a mask layer that can be closed

Pop-up windows are often used in actual developme...

Ubuntu 15.04 opens mysql remote port 3306

Ubuntu 15.04 opens MySQL remote port 3306. All th...