'Can not loop JSON object in Javascript

I apologize there is going to be a lot of attachments to explain this.

I am pulling a JSON object, storing it in SQLite, then later retrieve it and trying to compare it with a newer pull to check for updates. This works perfectly in simple, one level JSON. The trouble is with nested JSON.

  1. I console.log the datasets before comparison (the first array(4) is the new set, the second array(4) you see in this picture is the old set, previously retrieved from the Same API, stored in SQLite and then retrieved on future loading

datasets

they look identical and both are valid JSON objects according to jsonlint.com,

  1. I was able to loop through the new JSON and "collapse" it to a single level array so I can use it later to compare. With this code I can see that the "links" array is correct

new links array

        newData.forEach(m=>{
          console.log('new data loop 1');
          Object.entries(m as Record<string, string[]>).forEach(([key, value]) => {
            console.log('new data loop 2');

            if (key == 'links'){
              console.log('new value');
              console.log(value);
              
              for (let i=0; i< value.length; i++){
                newData2.push(value[i]);
              }
              // newData2.push(value);
            }
          })
        
        })
  1. However, when I tried to do the same loop for the old JSON object (stored in SQLite then retrieved), Chrome debugger thinks the "links" array has a length of zero, which from my research it could just mean it is recongzied as an object and therefore no length, never mind it worked fine for the new JSON object.

old links array

The issue: 4. I have no way to loop the old JSON object, here are three different ways to try to loop it and none of them makes into the loop.

 oldData.forEach(m2=>{
          console.log('old data loop 1');
          Object.entries(m2 as Record<string, string[]>).forEach(([key2, value2]) => {
            console.log('old data loop 2');

            if (key2 == 'links'){
              console.log('value');
              console.log(value2);
              
              
              for (var ii=0; ii< value2.length; ii++){
                console.log('old data loop 2.1');                
              }
              console.log('old data loop 2.5');
              
              value2.forEach(m3=>{
                console.log('old data loop 3');
                Object.entries(m3).forEach(([key3, value3]) => {
                  console.log('old data loop 4');
                  console.log(key3 + '...' + value3);
                  
                });
                
              })
              for (var key in value2) {
                console.log('old data loop 4');
                
                if (value2.hasOwnProperty(key) ){
                    console.log( "key:"+key+", val:"+value2[key] );
                }
             }
            }
          })
        
        })

The result looks something like this, so you can tell none of the attempts to loop the old "links" object works.

no loop happening 5. so there is obviously something about the old nested JSON object that is causing problems with the looping. I really appreciate any help as I have worked on this off and on for days and can't figure this out. The ultimate goal is to compare the two nested JSON objects so if there are other ways I am open to that too.



Solution 1:[1]

I solved this. The problem had nothing to do with how JSON objects is looped. It was an async problem. I did something after I load the array and I forgot to put that in a Promise so that even though Chrome debugger shows me the array had content, in real time it was empty!

Thanks @Serge, I did take your advice and just use string.search function now instead of trying to flatten the arrays manually then compare.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 O Dev