开发者

jquery associative array made up of variables

开发者 https://www.devze.com 2023-01-22 08:40 出处:网络
I have the following code: var paramTemp = ret.split(\'^\'); $.each(paramTemp, function(key, elem) { var splitTemp = elem.split(\'*\');

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 se开发者_开发技巧t a key to a variables value?

Thanks.


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消