Access JavaScript Object Variable Properties

By  on  

Not all JavaScript objects are as easy as MyObject.property. Sometimes you may want to access a property whose key is stored in a variable. Luckily accessing these properties is very easy.

Javascript Object Property Accessing Example

/* setting */
var myObject = {
	left : 30,
	top: 20
};
/* basic access */
var left = myObject.left; //OR
var left = myObject['left'];

/* accessing it or variables */
var mode = 'vertical';
var value = myObject[mode == 'horizontal' ? 'left' : 'top'];

You may use array-style syntax to access an object's properties. The string within brackets returns the properties.

Recent Features

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    Facebook-Style Modal Box Using MooTools

    In my oh-so-humble opinion, Facebook's Modal box is the best modal box around. It's lightweight, subtle, and very stylish. I've taken Facebook's imagery and CSS and combined it with MooTools' awesome functionality to duplicate the effect. The Imagery Facebook uses a funky sprite for their modal...

  • By
    Create Spinning, Fading Icons with CSS3 and MooTools

    A goal of my latest blog redesign was to practice what I preached a bit more;  add a bit more subtle flair.  One of the ways I accomplished that was by using CSS3 animations to change the display of my profile icons (RSS, GitHub, etc.)  I...

Discussion

  1. I’m a big fan of myObject[key] as its close to the PHP array syntax. Just personal preference really and it gives great flexibility and easy to test for.

  2. @Colin – I just love how object property keys and array indexes are treated as equal in Javascript. The closest to this coolness that PHP comes is in pseudo-property keys ($obj->$var_key)

  3. dev

    never knew this method, very useful.

  4. @Chris – PHP’s ArrayObject class allows you to access properties using array syntax.

  5. @keith – huh…never knew that. Thanks! :D

  6. It’s a good thing to point out that you can almost always avoid eval() by use of myObject[‘key’] reference. for example:

    var myObject = {};
    
    $H({
         'a': 1,
         'b': 2,
         'c': 3
    }).each(function(v, k) {
         myObject[k] = v;
    });
    

    A lot of people think you have to do something like:

    var myObject = {};
    
    $H({
         'a': 1,
         'b': 2,
         'c': 3
    }).each(function(v, k) {
         eval('myObject.' + k + ' = ' + v);
    });
    

    Avoid eval! Eval is evil.

  7. @Timothy: While it is preferable to avoid eval in code that need not be written in that form, as you show, I would like to contest that eval is far from evil.

    It’s a basic language construct upon which the entire functional programming paradigm can be built in languages that do not support it natively. Sometimes a problem is better expressed in said paradigm, but the language prevents you from properly exploiting it’s capabilities. This is one of the many ways eval *can* be used for the better of your programming speed, debug-ability and general style of programming.

    So please, next time you wish to share your opinion on eval, try to think of the grand picture this language construct fits in, and don’t spread lies about otherwise finely crafted implementations thereof.

  8. Thank you! I was stumped but this got me back on track.

  9. Ik

    Thank you soooo much, didn’t know this was possible. Spent countless hours trying to refactor my object to make calls easier, wish someone told me about this earlier lol.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!