22

I need to define a hash for posting some ajax data using jQuery. The hash will look something like:

var setname = 'set_1';
elements = { set_1: {'beer','water','wine'} };

The challenge I need to solve is 'set-1' (the key of Array elements) should be dynamically named based on the value of var setname.

I want to avoid using eval() of course.. in PHP it can be done using the double dollar sign like this: $$setname, but what's the way to do this in JavaScript?

4 Answers 4

35

You can do what you'd like to like so:

var setname = 'set_1', elements = {};
elements[setname] = ['beer','water','wine'];
alert(elements['set_1']); // beer,water,wine

See this in action at http://jsfiddle.net/x5KRD/.

All objects in JS can be accessed using dot notation (obj.method() or obj.property), or bracket notation (obj['method']() or obj['property']). Using bracket notation lets you dynamically specify method/property/key names.

For example, while clumsy, window['alert']('hi') is equivalent to window.alert('hi').

Note that your code won't work as-is, anyways, because you're using object literal notation ({'beer','water','wine'}) to contain an array (it should be in square brackets ['beer','water','wine'] instead). Object literals need to have key-value pairs.

Sign up to request clarification or add additional context in comments.

10 Comments

you're right.. my error, that was an edited simplified version, I actually have key value pairs in my actual object literal. so it works fine except for the dynamic assignment of the key name..
and I need to do this using a notaion that jQuery.post() will accepts for posting POST data..
@mikkelbreum I clarified what I was talking about.
thanks.. and I added that my code example had an error.. my actual code is: elements = { set_1: {drink1: 'beer', drink2: 'water', drink3: 'wine'} }; I need to do this using a notaion that jQuery.post() will accepts for posting POST data.. can I still use the notation elements[setname] = {drink1: 'beer', drink2: 'water', drink3: 'wine'} ? I'll try it, but I somehow feel it wont work..
@mikkelbreum - no you cannot do that with a simple expression. However, jQuery will accept the "data" value as an arbitrary object - it does not have to be in any "notation".
|
6
var setname = 'set_1', 
    elements = {};

elements[setname] = ['beer','water','wine'];

Comments

3

You have to understand that there's really no such thing as "JSON notation" in Javascript - it's native Javascript notation that we're talking about. What jQuery wants is a Javascript value for the POST data, not necessarily a Javascript object constant.

Thus, your code will prepare your POST data like this:

var elements = {};
elements[setName] = { /* something */ };
elements[somethingElse] = { /* something else */ };

and so on. When you're ready to do the POST, you'd just use "elements":

$.post(url, elements, function() { /* callback code */ });

Comments

2

Directly use the identifier in square brackets:

const setname = 'set_1';
const elements = { [setname]: ['beer','water','wine'] }

Please bear in mind the usage of square brackets for lists

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.