Converting javascript dictionary to array/object to be passed in jquery.params
Solution 1
The jQuery.param
method can take an array i in this form:
[{ name: 'asdf', value: '42' },{ name: 'qwerty', value: '1337' }]
If your dictionary have other property names, you can use the jQuery.map
method to convert it. Example:
arr = $.map(arr, function(e){
return { name: e.key, value: e.val };
});
Solution 2
If I understand your question right (some code samples would help!) by "dictionary" you mean a construction like:
var dict = { key1: value1, key2: value2 … }
or:
var dict = {};
dict[key1] = value1;
dict[key2] = value2;
or possibly:
var dict = {};
dict.key1 = value1;
dict.key2 = value2;
If so, you should know that all of these are doing the same thing, i.e. setting properties on JavaScript objects, and should be serialised correctly by jQuery.param
. Succinctly, JavaScript objects ≈ dictionaries.
Solution 3
use Object.values() :
var arr = Object.values(some_dict)
TopCoder
Updated on June 28, 2022Comments
-
TopCoder almost 2 years
I have a javascript variable which is a dictionary of key-values pairs of querystring. I need to again convert this dictionary into a query string. I am using jquery.param functin but it takes either an array or an object. How can I convert my dictinoary variable to array/object which jquery.params can accept.
Tried using serializeArray but it does not work.