8

I would like to create a json object to send as a post array, but I need to create the key on the fly

var id = $('#myInput').val();

var post = {
    'product[123]': 'myValue',     // this works fine - but isn't dynamic
    'product['+id+']': 'myValue'   // this does not work
}

Sending it in as a string works fine, but I get an issue when I want to make it more dynamic. Am I missing something really simple here, or am I trying to do something Javascript isn't supposed to do?

Eruant
  • 1,816
  • 2
  • 14
  • 22

3 Answers3

21

(Note that this has nothing to do with JSON. You're not using JSON there, you're using an object initializer. JSON is a textual (not code) format, which is a subset of JavaScript's object initializer syntax.)

Do it outside the object initializer, using [] notation:

var id = $('#myInput').val();

var post = {};
post[product[id]] = 'myValue';

That will take the value (at runtime) of product[id] and use that as the key for the property. If you wanted the key to literally be product[123] when id is 123, you'd use this instead:

post['product[' + id + ']'] = 'myValue';

A more generic discussion:

var a = "foo";
var obj = {};
obj[a] = "bar";
console.log(obj.foo); // "bar"

JavaScript allows you to specify property keys in two ways: Using dotted notation and a literal (obj.foo), or using bracketed notation and a string (obj["foo"]). In the latter case, the string doesn't have to be a string literal, it can be the result of any expression.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    I interpreted the question as him literally wanting an attribute called `product[123]`, but the question isn't clear at all... – Matt Jun 14 '12 at 15:46
  • @Matt: I see your interpretation, thanks. I wouldn't have read it that way, but now you've pointed it out... :-) – T.J. Crowder Jun 14 '12 at 15:49
0

Try

post['product[' + id + ']'] = 'myValue';
Mottie
  • 84,355
  • 30
  • 126
  • 241
0

Why do you use '[ ]' in ids of the object? Avoid to do this.
In your sample, you can do this by the following code:

var id = $('#myInput').val();
var post = {
    '123': 'myValue',    
    id: 'myValue' 
}

Or, if you realy realy want to use an arrry (actually, all objects ARE array in JavaScript). You can write this:

var product=[];
product['123']='something';
product[id]='another';
Jerry
  • 435
  • 4
  • 12
  • 1
    This will create an object (`post`) with an attribute `123` and an attribute `id` ([demo](http://jsfiddle.net/sH38n/)). Furthermore, there's nothing wrong with him using `[]` in the name of an attribute. – Matt Jun 14 '12 at 15:51
  • Your code creates an object with the property names `123` and `id`. It does **not** use the value of the `id` variable. – T.J. Crowder Jun 14 '12 at 15:51
  • Everything in JavaScript is an object, not the other way round; it is not true that all objects are arrays. – Matt Jun 14 '12 at 15:56
  • doesn't use the value of id? then what is the property name of the object ? – Jerry Jun 14 '12 at 15:56
  • @Jerry: See http://jsfiddle.net/sH38n/1/. It's `id`. You only need to quote a key in an Object Literal if it is a reserved word, or includes operators like `-`, `+` etc. – Matt Jun 14 '12 at 15:57
  • to Matt: everything is a object, this is very commonsense that we should not say it at all. But, I said every object in JS is a array, a "named array", you can use [ ] to get all its members. – Jerry Jun 14 '12 at 15:58
  • @Jerry: That's merely [square bracket notation](http://www.jibbering.com/faq/faq_notes/square_brackets.html) over dot notation. In JavaScript terminology an array is zero-indexed and has a `.length` property and is constructed via either `new Array` or `[]`. There's no concept of a `named array`, and it's use in terminology is [very misleading.](http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/) – Matt Jun 14 '12 at 16:01
  • the value of id is not used in my first code, I made a mistake here. About object as association array, I don't see any misleading. It is just another sytax for member access operator. – Jerry Jun 14 '12 at 16:15