0

I am trying out JS today and ran into the issue that the values of an array get lost when I convert it. The length of the JSON string is correct.

<script>
        console.log(arrayTmpre); // returns the correct array to console
        var testtwo = JSON.stringify(arrayTmpre); // should return the array as JSON readable string
        console.log(testtwo); // the result looks like this: [null,null,null] The values are missing!
        
</script>

This is the output in the console console log

Thanks in advance!

  • 2
    The array is still empty when you call `JSON.stringify`. Some asynchronous code (which you haven't shown us) is modifying it later. – Bergi Apr 10 '22 at 18:41
  • @Ivar sadly no, I tried logging it with "console.log(JSON.stringify(arrayTmpre));" like recommended - it returns the same string without values – nightmarelines Apr 10 '22 at 18:45
  • @Bergi Thanks that fixed it! I had another function that filled the array.The compiler seems to change the code order. So I just put the code snippets into different functions that call each other after they are done. – nightmarelines Apr 10 '22 at 18:57

0 Answers0