tags:

views:

60

answers:

3

I have a JavaScript object that I'd like to add some properties to, but I don't know what the names of the properties are until runtime.

Can I do this without using eval? If so, how?

var get_params = new Object();
var params = {'name':'john', 'age':'23'}; //actually not known until runtime
for (var i=0, len=params.length; i<len; ++i ){                                                  
        get_params.p[0] = p[1]; //How can I set p[0] as the object property?
    }
}
A: 
var get_params = {};
var params = [{'name':'john'}, {'age':'23'}]; 
for (var i=0,len=params.length; i<len; ++i){  
     for (var p in params[i]) {
        if(params[i].hasOwnProperty(p)) {
         get_params[p] = params[i][p]; 
        }
     }
}

Ok, that's my third version. I think it will do what I understand you to desire. Kind of convoluted however, and there are probably better formats for your dynamic array. If I understand what you want to do correctly, this should work. Basically, it creates the following object:

get_params = {
  name: "john",
  age: "23"
}
Tauren
This isn't working either. Here `p` is an object - eg `{'name':'john'}` - and `p[0]` doesn't exist. I'm quite sure OP meant for params to be `{ 'name': 'john', 'age': '23' }`, in which case your original solution was closer, but not correct. That's just me guessing, though, but either way, this isn't working. The result will currently be `get_params = { undefined: undefined }`
David Hedlund
Ahh, right. Little bit more complex, but certainly solvable...
Tauren
@David: I think the latest version will work. Without more details from the OP, that's as good as it gets.
Tauren
@Tauren: yes! now the result is what you say it'll be. if it's what OP wanted, I'm afraid we can only guess, so yeah, I'll agree this is as good as it gets, for the moment.
David Hedlund
@David, yep, its as good as it gets. But I did add a check for hasOwnProperty for thoroughness. What gets me is that the selected answer contains code that relies on an array access to an undefined element. Go figure...
Tauren
A: 

You can access objects via object['someKey'] as well.

elusive
A: 

Since your code example has a malformed array, I will include 2 variations.

Variation 1 (params is an actual object and not an array):

var get_params = {}; // prefer literal over Object constructors.

var params = {'name':'john', 'age':'23'}; // @runtime (as object literal)

for (var key in params){
    if(params.hasOwnProperty(key)) { // so we dont copy native props
        get_params[key] = params[key];
    }
}

Variation 2 (param is an array containing objects):

var get_params = {}; // prefer literal over Object constructors.

var params = [{'name':'john'},{'age':'23'}]; // @runtime (as array literal)

for(var i=0,param;param=params[i];i++) {
    for (var key in param){
        if(param.hasOwnProperty(key)) {
            get_params[key] = param[key];
        }
    }
}

Enjoy.

BGerrissen
So the first solution basically gets you the same thing as params. What's the point, why not just use the value of params itself? The second solution **appears** to work fine, but there is actually a reference to an undefined element in the array when i=2. This solution relies on javascript kicking you out of the for loop because you attempt to reference params[2], which is a non-existant element. As I said, it works, but it isn't the way I would want to do it.
Tauren