20

Say I have something that looks like this in javascripts:

var obj = {
  subObj : {}
};
var type = 'subObj';

How can I get obj's subObj w/ type? For instance I would like to do something like:

obj.(type);
0

5 Answers 5

39

obj[type]

You use subscript notation.

11.2.1 Property Accessors

Properties are accessed by name, using either the dot notation:

MemberExpression . IdentifierName
CallExpression . IdentifierName

or the bracket notation:

MemberExpression [ Expression ]
CallExpression [ Expression ]
Sign up to request clarification or add additional context in comments.

1 Comment

Updated link to Property Accessors documentation: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
7

You can treat objects like associative arrays in JavaScript, so you'll be able to access the inner object like:

var obj = {
    subObj : {}
};

var type = "subObj";
var subObj = obj[type];

Comments

2

In case someone was wondering how to access sub properties (dynamically), I have found a way for that, if there's an easier way, please let me know:

function getPropertyByKeyPath(targetObj, keyPath) { 
  var keys = keyPath.split('.');
  if(keys.length == 0) return undefined; 
  keys = keys.reverse();
  var subObject = targetObj;
  while(keys.length) {
   var k = keys.pop();
   if(!subObject.hasOwnProperty(k)) {
    return undefined;
   } else {
    subObject = subObject[k];
   }
 }
 return subObject;
}

For example this:

      var o = {result : {info:{ version:1, comment: 'test'}}};
      var subObject = getPropertyByKeyPath(o, 'result.info');
      console.log(subObject);

would result in:

{version: 1, comment: "test"} 

Comments

0
obj[type]

doesnt make any sense at all - you're not accessing TYPES, these are simple PROPERTIES - one could access them via obj[keyName] or something similar but using type is extremely confusing and a sure sign of lack of understanding

1 Comment

Well, properties are types. If you "console.log(typeof(obj.subObj));", it will return "object" to stdout. However properties can be accesses either by "obj.subObj" or "obj['subObj']" not "obj[subObj]". The latter will not work because the name of the object in brackets must be presented as a string.
-3
    var obj = {
      subObj : {}
    }
    var type = 'subObj';

    console.log(typeof obj.subObj); //will print "object"
    console.log(obj.subObj); //will print "{}"

    //You can add a new propery like this:
    obj.subObj.newProperty = 'hello';
    console.log(typeof obj.subObj.newProperty); //will print "string"
    console.log(obj.subObj.newProperty); //will print "hello"

    // you can also add new properties like this
    obj.subObj['newProperty'] = 5; // this is very useful if you have to add properties with names that use reserved words or that contain illegal characters.
    console.log(typeof obj.subObj.newProperty); //will print "number"
    console.log(obj.subObj.newProperty); //will print "5"

    //In the case where you're using reserved or illegal words you'l have to do this  
    obj.subObj['new/Property'] = 'cat'; // this is very useful if you have to add properties with names that use reserved words or that contain illegal characters.
    console.log(typeof obj.subObj['new/Property']); //will print "number"
    console.log(obj.subObj['new/Property]); //will print "5"

    // and add another property in  the chain:
    obj.subObj['new/PropertyGroup']={
'illegal/name':'value', 
acceptableName: 5, 
andotherObject:{},
'illegally&Named(object)':{} 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.