0

I'm mentioning a strange behavior:

This is my source string: {"data":{"allowfullscreen":true,"duration":0,"keyboard":false,"imgclass":"","hrefclass":""},"images":[{"src":"storage/home-hero.jpg","alt":"Home hero"}]}

Now I'm doing this:

parsed = {};
try {
    parsed = JSON.parse(data.matches[1]);
    } catch (e) {}
console.log(JSON.parse(data.matches[1]));
console.log(parsed);

the results should be the same, right?

But the output is different:

Output #1 (JSON.parse(data.matches[1]);)

Object

data: {allowfullscreen: true, duration: 0, keyboard: false, imgclass: "", hrefclass: ""}

images: [{src: "storage/home-hero.jpg", alt: "Home hero"}]

and Output #2 (parsed):

Object

images: Array (1)
0 {src: "storage/home-hero.jpg", alt: "Home hero", __v-for__8: null}

Why aren't they the same?

SPQRInc
  • 162
  • 4
  • 23
  • 64
  • I have serious doubts that that's really happening. Is there really **no** other code involved? What does the source string look like? – Pointy Jan 29 '18 at 20:11
  • 3
    Are you looking at the console immediately, or after other code has modified `parsed`? The console is live, so changes to the object will be reflected when you view the object later. – Barmar Jan 29 '18 at 20:11
  • 1
    Are you using Vue? If `parsed` is assigned to a variable that vue will use then vue will also modify your object to increase its functionality. And since the console is live, as Barmer said, you're not getting a snapshot of parsed, but rather the updated form of it at whatever time you decide to physically open the console and look at the object. – Khauri Jan 29 '18 at 20:14
  • Why is this marked as duplicate? I also added the source string to the question. – SPQRInc Jan 29 '18 at 20:14
  • Yes, I am using Vue... Alright, this is a good hint. – SPQRInc Jan 29 '18 at 20:15
  • It's marked as a duplicate because it's a duplicate. The issue isn't related to the parser but rather to a misunderstanding of how a console will display an object. Put a breakpoint after the second `console.log()` so that you can examine the console *before* the code continues. –  Jan 29 '18 at 20:16
  • Alright - that got me on the right way. Thanks a lot. As I'm new to JS I always thought it's printed out in the state it is in the moment of execution. The breakpoint helped me. – SPQRInc Jan 29 '18 at 20:39

0 Answers0