2

I'm having a bit of difficulty with transferring a JSON object from the server to the client-side javascript

I have a rows of data fetched from MySQL query stored into $result

Here's the code:

var json = '<?= json_encode($result->fetch_all()) ?>';
var word_list = JSON.parse(json);
console.log(word_list);     //index.php:23
console.log(json);          //index.php:24

This is the result in chrome console: number of arrays in the concise version and expanded version don't match

Can someone tell me:
1. Why line 23 shows length of 5 when folded, then only show length of 4 when unfolded?
2. Where did word5 go? FYI, the word that disappears changes every time I refresh.

3
  • maybe you have to scroll down?? Commented Jun 28, 2015 at 10:52
  • @Grumpy Nope. That's it. that's the expanded version Commented Jun 28, 2015 at 11:08
  • I am able to reproduce this, but I doubt your code above is what is causing this, unless you have more code we are not seeing. I'm going to post a way to reproduce this... Commented Jun 28, 2015 at 11:08

2 Answers 2

3

I am able to reproduce your screenshot.

screenshot

I purposely hovered over the little "i" to reveal its tooltip. It states "Object state below is captured upon first expansion.

This means that if you print the array, don't expand it, modify the array (say with a pop()) then when you expand it you will see the modified array.

Demo: JSBin

Sign up to request clarification or add additional context in comments.

Comments

1

console.log logs your state of the object to the console when it is hitting the console.log while inspecting the array (or any object) shows you the current state of it.

var a = [1,2,3,4,5].map(function(){ return new String(); });
console.log(a);
a.pop();

enter image description here

1 Comment

Thanks for your input!! That definitely helped a lot. It got me out of the hole I was in. Now I know where to look for the little bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.