0

So I have an object that looks like:

var computer={
"Home":{
    "Applications":{
        "Desktop":{

        },
        "Documents":{

        },
        "Downloads":{

        },
        "Library":{

        },
        "Movies":{

        },
        "Music":{

        },
        "Pictures":{

        }
    }
},
"Library":{

},
"Downloads":{

},
"Files":{

},
"Devices":{
    "USB":{

    },
    "Hard_Drive":{

        }

    }
}

I also have an input box that should receive the input from the user to location. Then looking up that location using console.log(computer[location]); should show all of the children of that object. So the basic idea is this:

var location = document.getElementById("inputId");
console.log(computer[location]);

What are some patterns I can use to make this work? I'm also open to changing the idea of having computer as a object or any other changes. I just need to make this work. Thanks in advance.

Julian
  • 2,814
  • 21
  • 31
eclipseIzHere
  • 83
  • 1
  • 7
  • 1
    `document.getElementById("inputId").value;`...Access `.value` property... – Rayon Sep 14 '16 at 17:08
  • but even then, if i have this var obj={ Home:{ Value:"value is value" } } var val="Home"; var th=String(obj[val]); console.log(th); it still returns object Object, not the actual value of the thing – eclipseIzHere Sep 14 '16 at 17:10
  • Have what ? Accessing the value from the object is your problem ? – Rayon Sep 14 '16 at 17:11
  • `obj[val]` contains another `Object`, remove `Sting` and check the console... – Rayon Sep 14 '16 at 17:12
  • @Rayon yea, accessing the value from the object is what i can't do and wanna do. if i try to get a the value of something within a object within computer, it returns undefined. – eclipseIzHere Sep 14 '16 at 17:17
  • Make sure you are accessing a key which is present in the object... – Rayon Sep 14 '16 at 17:18
  • var obj={ Home:{ Value:"value is value" } } var val="Value"; var th=String(obj[val]); console.log(th); i'm accessing Value in object obj, but it returns undefined – eclipseIzHere Sep 14 '16 at 19:25
  • check [this](http://stackoverflow.com/a/37512722/4543207) up. – Redu Sep 15 '16 at 08:25

1 Answers1

0

I think i finally got it. This code will loop through the object(s), and if the string is found, set the value to the 'target' variable.

        var obj = {
            name: "Darin",
            age: 30,
            occupation: "Programmer",
            employers: {
                burgerKing:{
                    name: "Burger King",
                    startDate: "2001",
                    endDate: "2006"                 
                }
            }
        }   



        var target = null; //the final value is stored here

        function finder(obj, key){

            $.each(obj, function(k,v){

                if(typeof v == "object"){
                    finder(v, key); 
                }
                else{
                    if(k == key) target = v;
                }           
            });     
        }


        finder(obj, "startDate");
Darin Cardin
  • 627
  • 1
  • 4
  • 10
  • what does i and v (the params) do? param i is the value of the input and v is a function that is called from what i understand. but i is never used and v isn't a function. – eclipseIzHere Sep 14 '16 at 19:24
  • $('button').click(function(i,v) i: the event, v: in this case it does nothing, I copy/pasted it from another project. Here is the documentation on jQuery clicks: https://api.jquery.com/click/ – Darin Cardin Sep 14 '16 at 21:11
  • Not exactly. In the 'each' function, you still need the v, because it has the value that is the key for the object: $(val).each(function(i,v). You can remove the v from the click function, as mentioned above. In your fiddle, it appears that the code is failing because the v is missing from the each. – Darin Cardin Sep 15 '16 at 01:05
  • Here is a working fiddle: https://jsfiddle.net/Darin_Cardin/8b2fo9s9/2/ Let me know if this solves your problem. – Darin Cardin Sep 15 '16 at 01:14
  • no, what i wanna do is the user will enter a value (startdate for example and there will be nothing with same name in my object), just entering "startdate" (without quotes) and it will return it be it is nested a million times. – eclipseIzHere Sep 15 '16 at 16:57
  • it still returns undefined :( – eclipseIzHere Sep 15 '16 at 22:53
  • it still returns null on console.log but it works in jsfiddle .-. – eclipseIzHere Sep 16 '16 at 00:48
  • So does the fiddle solve your problem? I dont want to spend too much more time on this issue. :) – Darin Cardin Sep 16 '16 at 04:22