1

I have the following code:

var paramTemp = ret.split('^');
    $.each(paramTemp, function(key, elem) {
      var splitTemp = elem.split('*');
      params = {
    splitTemp[0]: splitTemp[1]
      };
    });

I get complaints when I try to set the key to splitTemp[0]. How do I set a key to a variables value?

Thanks.

1
  • FYI - Javascript doesn't have associative arrays. Associative arrays are actually objects, but due to the way Javascript allows alternate syntax (such as in Nick Craver's post), you can use array syntax to reference object variables. This has no affect on how to fix your problem, but I just thought I'd share. Commented Oct 29, 2010 at 16:46

1 Answer 1

1

You do this using bracket notation, it should look like this:

var paramTemp = ret.split('^'), params = {};
$.each(paramTemp, function(key, elem) {
  var splitTemp = elem.split('*');
  params[splitTemp[0]] = splitTemp[1];
});

In JavaScript these have the same effect:

obj.name = value;
obj["name"] = value;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.