Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Not sure if this can even be done, but I'll ask anyway:

Suppose if I have an array of names:

['bob', 'sue', 'dan']

And I want to dynamically create an object from those names:

bob.sue.dan = 5;

Is it possible?

share|improve this question

5 Answers 5

up vote 2 down vote accepted

Here you go, will preserve existing objects:

var namespace = function(name, separator, container){
  var ns = name.split(separator || '.'),
    o = container || window,
    i,
    len;
  for(i = 0, len = ns.length; i < len; i++){
    o = o[ns[i]] = o[ns[i]] || {};
  }
  return o;
};

e.g. usage:

namespace("com.example.namespace");
com.example.namespace.test = function(){
  alert("In namespaced function.");
};

or for your example using an array.

var ns = ['bob', 'sue', 'dan'];
namespace(ns.join('.'));
bob.sue.dan.foobar = true;

or extending an existing object:

var bob = {}
namespace("foo.bar",".",bob);
bob.foo.bar = true;

Edit: updated as requested:

var namespace = function(name, separator, container, val){
  var ns = name.split(separator || '.'),
    o = container || window, i, len;
  for(i = 0, len = ns.length; i < len; i++){
      var v = (i==len-1 && val) ? val : {};
      o = o[ns[i]] = o[ns[i]] || v;
  }
  return o;
};

namespace("bob.sue.dan",null,null,5);
alert(bob.sue.dan);

See working example: http://jsfiddle.net/herostwist/hu6j9/

share|improve this answer
    
I like the elegance of your solution a lot. Is there a way to edit it to give a value in the function itself? –  Manny Fleurmond May 13 '11 at 7:54
    
for example: namespace(name, seperator, container, value) –  Manny Fleurmond May 13 '11 at 8:23
    
updated to accept optional value parameter :) –  herostwist May 13 '11 at 9:03
    
thank you. Not 100% sure how it works, but it does and does well. –  Manny Fleurmond May 13 '11 at 9:22
var names = ['bob', 'sue', 'dan'];
var objs = [];

for(var i=0; i<names.length; i++) {
  objs.push(names[i]);
  var val = (i==names.length-1) ? "5" : "{}";
  eval(objs.join(".") + " = " + val);
}

alert(bob.sue.dan);

Demo: http://jsfiddle.net/EpZm2/1/

share|improve this answer

sure you can ...

var obj = 5;
var yourarray = ['bob', 'sue', 'dan'];

yourarray = yourarray.reverse();
for(e in yourarray) {
    var tmpobj = obj;
    obj = new Object();
    obj[yourarray[e]] = tmpobj;

    // if you already have an object
    if (e+1 == yourarray.length) {
        your_current_existing_object[yourarray[e]] = tmpobj;
    }
}
share|improve this answer
    
What would happen if I already had an object with some populated properties? –  Manny Fleurmond May 13 '11 at 7:07
    
Don't use for..in with arrays unless there is a very good reason and you have protected against the foibles. –  RobG May 13 '11 at 7:11

Then you can do:

function makeOjectTree(propNames) {
  var name;
  var o = {};
  var result = o;

  for (var i=0, iLen=propNames.length; i<iLen; i++) {
    name = propNames[i];

    if (!o[name]) {
      o[name] = {};
      o = o[name];
    }
  }
  return result;
}
share|improve this answer

Yes this is possible.

You can define new properties on an object this way:

var obj = {};
obj["bob"] = {};
obj["bob"]["sue"] = {};
obj["bob"]["sue"]["dan"] = 5;

So you can also do it with an array of property names ;)

share|improve this answer
1  
that's not dynamically created object –  Teneff May 13 '11 at 7:04
    
especially since I don't want to disturb any other properties that are already in other levels of the structure –  Manny Fleurmond May 13 '11 at 7:06
    
@Teneff I never said it is a dynamically created object. I only showed you the way you could define properties dynamically by a string. And that´s exactly what you need ;) I think -1 is not fair... –  Van Coding May 13 '11 at 7:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.