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"
3
  • 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 Commented Dec 4, 2013 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? Commented Dec 4, 2013 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. Commented Dec 5, 2013 at 4:09

2 Answers 2

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.

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

3 Comments

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.
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.
It's the fourth sentence in the original question, "I need deep serialization and also url encoding"
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();
  }
}

2 Comments

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.
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.

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.