0

I was asked in my other question to ask the question more generally which is what this question is, if you would like me to modify the other question to be this question word for word, please tell me so.

I will attempt to generalize my question as much as possible here, but i am not all that familiar with the .json document myself so this will be a bit of stretch.

So, I have a .json document with the object i need (id) and the object i have (name), they are both inside of another object. Something like this (Majorly simplified version):

{"LEA":
{"name": "Limited Edition Alpha", "code": "LEA", "cards":[
{"name": "Sorin", "id": "02389302"},

"SOI":
{"name": "Shadows over Innistrad", "code": "SOI", "cards":[
{"name": "Avacyn", "id": "12394381"}
}

I am attempting to cycle through every single object (the 'LEA', 'SOI' part - there are 192 of them) until I come across the object I have (for example 'Avacyn'), and then locate the object I need (in this case '12394381'), i am currently at the stage of cycling through the objects:

        $.getJSON("AllSets.json", function(json) {

            console.log(json); 

            Sets = Object.keys(json);
            i = 0;


            while (i < Sets.length){
                currentSet = Sets[i];
                console.log(json.currentSet);
                console.log(currentSet);
                console.log(i);
                i = i + 1;

            }


        });

But I have hit a snag as the currentSet and i variable are returning the correct values, but the json.currentSet is returning undefined I believe this is as it is reading it as the variable name and not the variable contents.

All i really need to know is, am i approaching this correctly? is there an easy way to do this? Am i completely off track? I am not a javascript coder and i am not familiar with .json, I am simply trying to get the id from the name that i currently have as a string.

EDIT:

Complete code below, thank you very much to @Felix Kling for all the amazing resources. The key was to move through each object one item at time.

        $.getJSON("AllSets.json", function(json) {

            Sets = Object.keys(json);
            i = 0;

            while (i < Sets.length){

                currentSet = Sets[i];
                Cards = Object.keys(json[currentSet].cards);
                j = 0

                while (j < Cards.length){

                    currentCard = Cards[j];
                    cardName = json[currentSet].cards[currentCard].name;
                    if (cardName == "Island"){
                        console.log(json[currentSet].cards[currentCard].multiverseid);

                    }
                    j = j + 1
                }

                i = i + 1;

                }
            }


        });
Matt Cornell
  • 25
  • 1
  • 5
  • These might also be interesting for you: [How do I loop through or enumerate a JavaScript object?](http://stackoverflow.com/q/684672/218196) and [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196) – Felix Kling Jun 08 '16 at 16:40
  • 1
    Thank you very much for the links, after reading through i was able to complete the program, i think what was confusing me the most the past couple of hours was the syntax, thank you very much for these amazing resources! I will add the completed code to the end of the question for anyone who wants it. – Matt Cornell Jun 08 '16 at 20:52

0 Answers0