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

How to solve the problem of MySQL query character set mismatch

Find the problem I recently encountered a problem...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

PHP related paths and modification methods in Ubuntu environment

PHP related paths in Ubuntu environment PHP path ...

HTML Learning Notes--Detailed Explanation of HTML Syntax (Must Read)

1. What is HTML markup language? HTML is a markup...

Implementation steps for docker deployment of springboot and vue projects

Table of contents A. Docker deployment of springb...

Summary of basic usage of js array

Preface Arrays are a special kind of object. Ther...

MySQL 5.7.23 installation and configuration method graphic tutorial

This article records the installation tutorial of...

Basic knowledge of MySQL learning notes

View Database show databases; Create a database c...

How to install JDK8 on Windows

1. Download: http://www.oracle.com/technetwork/ja...

Detailed explanation of common usage methods of weixin-js-sdk in vue

Link: https://qydev.weixin.qq.com/wiki/index.php?...

Sample code for making a drop-down menu using pure CSS

Introduction: When I looked at interview question...

Summary of Vue 3 custom directive development

What is a directive? Both Angular and Vue have th...

Markup Language - Image Replacement

Click here to return to the 123WORDPRESS.COM HTML ...