CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2019-07-17
by Vencovsky

Original Post

Original - Posted on 2012-07-23
by Rob W



            
Present in both answers; Present only in the new answer; Present only in the old answer;

What happens here is that `JSON.stringify` cannot be used for circular data. Circular data is when you have an object that references some other parent object. If `JSON.stringify` printed some circular data, it would be a infinity string.
This probably happen because you are getting some circular data from the response.
If you really want to print it and ignore the circular that, you can take a look at [this question](https://stackoverflow.com/questions/11616630/how-can-i-print-a-circular-structure-in-a-json-like-format) that have alot of ways to do so.
What I recommend is using `console.log` instead of `alert` and in the console you will be able to see circular data with no problem.
Here is a demo for showing circular data, wich is taken from [this answer](https://stackoverflow.com/a/11616993/9119186).
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Demo: Circular reference var o = {}; o.o = o;
// Note: cache should not be re-used by repeated calls to JSON.stringify. var cache = []; JSON.stringify(o, function(key, value) { if (typeof value === 'object' && value !== null) { if (cache.indexOf(value) !== -1) { // Duplicate reference found, discard key return; } // Store value in our collection cache.push(value); } return value; }); cache = null; // Enable garbage collection
var a = {b:1} var o = {}; o.one = a; o.two = a; // one and two point to the same object, but two is discarded: console.log(JSON.stringify(o))


<!-- end snippet -->

Use `JSON.stringify` with a custom replacer. For example:
// Demo: Circular reference var o = {}; o.o = o;
// Note: cache should not be re-used by repeated calls to JSON.stringify. var cache = []; JSON.stringify(o, function(key, value) { if (typeof value === 'object' && value !== null) { if (cache.indexOf(value) !== -1) { // Duplicate reference found, discard key return; } // Store value in our collection cache.push(value); } return value; }); cache = null; // Enable garbage collection
The replacer in this example is not 100% correct (depending on your definition of "duplicate"). In the following case, a value is discarded:
var a = {b:1} var o = {}; o.one = a; o.two = a; // one and two point to the same object, but two is discarded: JSON.stringify(o, ...);
But the concept stands: Use a custom replacer, and keep track of the parsed object values.

        
Present in both answers; Present only in the new answer; Present only in the old answer;