I’m having a problem researching objects inside objects

Asked

Viewed 34 times

1

I download a JSON file, by example, saved and then open (all this by javascript), and want to access the Property 'Has_infobox_html', I tried to use the following loop

var poeItem = new function() {
    this._getHTMLdata = function(fileData) {
        for (i = 0; i = fileData.query.data.length; i++); {
            if (fileData.query.data[i] && fileData.query.data[i].property == 'Has_infobox_HTML') {
                return fileData.query.data[i].dataitem[0].item
            }
        }
    }
}

However it does not return anything, I tried to print the 'i’s and it seems to test only for 0 and then for, I have tested the other parts of the code and they work normally, the problem seems to be in this specific part

  • what you want to return? take care that when the if der true the return is breaking the for

1 answer

0

There were two syntax errors in your script, very silly thing.

 for (i = 0; i = 

The equals sign there causes an infinite loop. Use the <.

for (i = 0; i < fileData.query.data.length; i++); {

And it has a ; after the declaration of the loop.

Your corrected code:

 var poeItem = new function() {
     this._getHTMLdata = function(fileData) {
         for (var i = 0; i < fileData.query.data.length; i++) {
             if (fileData.query.data[i] && fileData.query.data[i].property == 'Has_infobox_HTML') {
                 return fileData.query.data[i].dataitem[0].item
             }
         }
     }
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.