0

How can I parameterize an array so that it's a single name value pair that's comma separated. Using jQuery $.param creates a parameter for each array value instead of just one. And apparently there's no option or setting to change this. I'm looking for more than just using Array.join since I need deep serialization and also url encoding. Is there an jQuery.param option or utility library for this?

Using jQuery's param method:

$.param({ a: [2, 3, 4] }); // "a[]=2&a[]=3&a[]=4"

I need:

var p = {a: [2, 3, 4]};
param(p) //"a=2,3,4"
jEremyB
  • 1,736
  • 12
  • 15
  • you can get rid of the brackets by setting the traditional param to true, but it still isn't quite in the form you want. `$.param({ a: [2, 3, 4] },true); // a=2&a=3&a=4` – Kevin B Dec 04 '13 at 20:39
  • Maybe it would be more appropriate in your case to serialize your data structure to a JSON string and send that string instead, since the structure is so important to you? – Kevin B Dec 04 '13 at 20:40
  • The serialized parameter is being used in a url that's put in the body of an email (using mailto:). I wouldn't be able to use json since it would require POST and not GET. – jEremyB Dec 05 '13 at 04:09

2 Answers2

2

At its simplest, and assuming the value of each property of the object is an array:

function param(obj) {
    var output = [];
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            output.push(prop + '=' + obj[prop].join(','));
        }
    }
    return output.join('&');
}

var p = {
    a: [2, 3, 4],
    b: ['something','else']
};
console.log(param(p)); //a=2,3,4&b=something,else

JS Fiddle demo.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • I mentioned I need deep serialization and url encoding. To accept this solution it'd also need to recursively check for nested arrays and encode output with encodeURIComponent. – jEremyB Dec 05 '13 at 04:16
  • When you asked the question those requirements were left out. I'm happy to try and improve it, later, but I'm unlikely to have the time to do so for quite a while today. Incidentally, if you have specific requirements, add those to the question; people don't always read all of the comments to a question. – David Thomas Dec 05 '13 at 06:52
  • It's the fourth sentence in the original question, "I need deep serialization and also url encoding" – jEremyB Dec 07 '13 at 05:09
0

I think you're going to have to create this function bespoke. Fortunately if your data structure is that simple then it's not too difficult:

var p = {a: [2, 3, 4]};
var encoded = '';

for (var prop in p) { 
  if (p.hasOwnProperty(prop)){
    encoded += prop + '=' + p[prop].join();
  }
}
Jivings
  • 22,834
  • 6
  • 60
  • 101
  • I thought this function would be available in a common library like jquery or jquery BBQ plugin. Serializing arrays as comma separated values must be a non standard way for the server to accept input. – jEremyB Dec 05 '13 at 04:20
  • In coldfusion, a=2&a=3&a=4 becomes a single variable that contains a list, like you are looking for. it just depends on how the server interprets it. If it has brackets, i'll get an array of values. – Kevin B Dec 05 '13 at 04:21