Top community members
All Wiki Articles Create Wiki Article

Many years me and my friends we were volunteers on my university doing programming classes for young people.

I got main conclusion: Young people are awesome, especially students, because they always bring new and fresh ideas :)
* I loved to do it but my time is limited

  I decided to create something that will help millions of students

Now I am here

co-founder

JavaScript - how to dynamically access object property with variable?

0 contributions
7 points

In JavaScript there are avaialble two ways to access object property:

  • dot notation, e.g. object.property
  • square bracket notation, e.g. object['property']

1. Bracket notation property access example

// ONLINE-RUNNER:browser;

var object = {
    'key 1': {
        name: 'John',
        age: 25
    },
    'key 2': {
        name: 'Kate',
        age: 32
    }
};

var variable = 'key 2';
var person = object[variable];

console.log('name: ' + person.name + ', age: ' + person.age);

Output:

name: Kate, age: 32

Note: using square bracket notation it is possile to use property characters that are not permited for dot notataion (inside above example space character has been used).

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more