-1

I am just learning to play with javascript and I wanted to be able to access a object I have via a variable name.

My data object is

    var browser =  ['chrome', 'safari', 'firefox', 'ie'];

  var browser_tasks = {
        chrome : 'chrome',
        firefox : 'firefox',
        ie :'internet explorer',
        safari : 'safari'

    };

and I want to be able to access it like

for (var i=0; i<browser.length;i++){
console.log(browser_tasks.browser[i]);
}

Is there a way to do this programmatically?

user3626708
  • 727
  • 2
  • 8
  • 24

1 Answers1

1

Yep - you need bracket notation when accessing an objects properties via a variable:

console.log(browser_tasks[browser[i]]);
tymeJV
  • 103,943
  • 14
  • 161
  • 157