0

This is probably trivial, but i couldn't find an answer.

I have this object:

        obj = {
            key1 : {val : "hi", val2 : "pizza"},
            key2 : {val : "hello", val2 : "cheese"},
            key3 : {val : "wazzup", val2 : "hamburger"}
        };

And this function:

        function foo () {
            $.each(obj, function(key, value) {
                console.log(value.val2);
            });
        }

        foo();

This works quite fine.

Question: how can I access one specific property passing it as an argument? For example:

    function foo (arg) {
        $.each(obj, function(key, value) {
            console.log(value.arg);
        });
    }

    foo(val2);
Enrico
  • 408
  • 6
  • 13

1 Answers1

3

Try to use bracket notation,

function foo (arg) {
    $.each(obj, function(key, value) {
        console.log(value[arg]);
    });
}

foo(val2);
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130