Using a variable as identifier in a json array

Posted by Robbert van den Bogerd on Stack Overflow See other posts from Stack Overflow or by Robbert van den Bogerd
Published on 2010-04-29T14:50:16Z Indexed on 2010/04/29 14:57 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

Hi folks!

I'm wondering if it is possible to use assigned variables as identifier in a json array. When I tried this, I was getting some unexpected results:

(Code is simplified, parameters are passed in a different way)


var parameter = 'animal';
var value = 'pony';

Util.urlAppendParameters (url, {parameter : value});


Util.urlAppendParameters = function(url, parameters) {
    for (var x in parameters) {
        alert(x);
    }
}

Now the alert popup says: 'parameter' instead of 'animal'. I know I could use a different method (creating an array and assigning every parameter on a new line), but I want to keep my code compact.

So my question is: Is it possible to use a variable as an identifier in the json array, and if so, could you please tell me how?

Thanks in advance!

© Stack Overflow or respective owner

Related posts about JSON

Related posts about variable