开发者

Converting javascript dictionary to array/object to be passed in jquery.params

I 开发者_JAVA百科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.


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 };
});


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.


use Object.values() :

var arr = Object.values(some_dict)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜