Rather Notsay Rather Notsay - 3 months ago 12
Javascript Question

How would I construct an object array so that I could reference properties in this way?

My app has multiple screens and I want to store properties for each screen in an object. I want to be able to reference it easily by name, for instance:

var homelabel = screen('home').label;


or

var settingsIcon = screen['settings'].icon;


How would this be done?

Answer Source

let screen = {
  "home": {
    "icon": "home_icon.png",
    "label": "home_label"
  },
  "settings": {
    "icon": "settings_icon.png",
    "label": "settings_label"
  }
}

console.log(screen["home"].label);
console.log(screen["settings"].icon);