1

I'm trying to serialize to URL string an associative array whose elements are defined at runtime in the form of arr[key]=value, where key and value are determined at runtime, but arr is predefined as a global before runtime. I can't get the param() function to work with this kind of array. Is there another function to use to serialize such arrays?

var arr=[];
arr["apple"]="poisoned";
arr["banana"]="digested";

var str=jQuery.param(arr);
$("#results").text(str);
​
Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
ina
  • 19,167
  • 39
  • 122
  • 201
  • 1
    look to this question: http://stackoverflow.com/questions/3462087/passing-an-nested-arrays-to-asp-net-mvc-using-jquerys-ajax/3463129 # 3463129 – andres descalzo Aug 14 '10 at 04:15

1 Answers1

1

Use object notation instead:

var arr={};
arr["apple"]="poisoned"; // or arr.apple
arr["banana"]="digested"; // or arr.banana

While objects and arrays operate in a similar same way in JS its still better to think of arrays as being numerically indexed arrays, as opposed to hashes.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • hmm if it's an object rather than array - would there be any trouble with using `delete` and `pop()` and such to manipulate it? – ina Aug 14 '10 at 03:13
  • `delete` will be fine. `pop` will not work unless you create your own method and augument the object... But if you need named keys why are you using `pop`/`push`? I think this has become a more specific question about what you are trying to do... would you care to elaborate on what you are trying to implement? – prodigitalson Aug 14 '10 at 03:19