JavaScript 数组到 URLencoded

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4656168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:29:28  来源:igfitidea点击:

JavaScript Array to URLencoded

javascripturlencode

提问by theHack

is there any js function to convert an array to urlencoded? i'm totally newbie n JS... thanks!...

是否有任何 js 函数可以将数组转换为 urlencoded?我完全是新手和 JS ......谢谢!......



my array is a key & value array.... so,

我的数组是一个键和值数组......所以,

myData=new Array('id'=>'354313','fname'=>'Henry','lname'=>'Ford');

is the same as

是相同的

myData['id']='354313';
myData['fname']='Henry';
myData['lname']='Ford';
myData.join('&'); //returns error, it doesn't work on such array...

is there any solution?

有什么解决办法吗?



oh sory... i have an array like this

哦,对不起......我有一个这样的数组

var myData=new Array('id'=>'354313','fname'=>'Henry','lname'=>'Ford');

then i need the array converted to be:

然后我需要将数组转换为:

id=354313&fname=Henry&lname=Ford

采纳答案by はると

You can do something like this:

你可以这样做:

var myData = new Array('id=354313', 'fname=Henry', 'lname=Ford');
var url = myData.join('&');

回答by jgrund

Try this:

尝试这个:

var myData = {'id': '354313', 'fname':'Henry', 'lname': 'Ford'};
var out = [];

for (var key in myData) {
    if (myData.hasOwnProperty(key)) {
        out.push(key + '=' + encodeURIComponent(myData[key]));
    }
}

out.join('&');

For an explanation about why use hasOwnProperty, take a look at this answer to "How do I loop through or enumerate a JavaScript object?".

有关为什么使用的解释hasOwnProperty,请查看“如何循环遍历或枚举 JavaScript 对象?”的答案。.

回答by Eduard Brokan

If you use jQuery, can use $.param(). Check here. Example of using

如果你使用 jQuery,可以使用 $.param()。检查这里。使用示例

var myData = {'id' : '354313', 'fname' : 'Henry', 'lname' : 'Ford'};
var url = "https://stackoverflow.com?" + $.param(myData);

Output is

输出是

https://stackoverflow.com?id=354313&fname=Henry&lname=Ford

Also works with an array of objects (like from jQuery(form).serializeArray()) :

也适用于对象数组(如来自jQuery(form).serializeArray()):

var myData = [{'id' : '354313'}, {'fname' : 'Henry'},{'lname' : 'Ford'}];

回答by romiguelangel

If you use an object instead of an array you can do this (ES6) :

如果您使用对象而不是数组,您可以这样做(ES6):

var myData = {
    id: 354313,
    fname: 'Henry',
    lname: 'Ford',
    url: 'https://es.wikipedia.org/wiki/Henry_Ford',
};

encodeDataToURL = (data) => {
    return Object
      .keys(data)
      .map(value => `${value}=${encodeURIComponent(data[value])}`)
      .join('&');
 }

console.log(encodeDataToURL(myData));

回答by Makord Sp

var myObject = {
  a: {
    one: 1,
    two: 2,
    three: 3
  },
  b: [ 1, 2, 3 ]
};
var recursiveEncoded = $.param( myObject );
var recursiveDecoded = decodeURIComponent( $.param( myObject ) );

alert( recursiveEncoded );
alert( recursiveDecoded );

The values of recursiveEncoded and recursiveDecoded are alerted as follows:

recursiveEncoded 和 recursiveDecoded 的值警告如下:

a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3 a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3

a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3 a[one]=1&a[two]=2&a[three ]=3&b[]=1&b[]=2&b[]=3

https://api.jquery.com/jQuery.param/

https://api.jquery.com/jQuery.param/

回答by Vincent

Look at the function escape and unescape.

查看函数 escape 和unescape

回答by SEoF

Taken from jgrunds answer, if you want to extend the array functionality

取自 jgrunds 答案,如果您想扩展数组功能

Array.prototype.toQueryString = function(){
    var out = new Array();

    for(key in this){
        out.push(key + '=' + encodeURIComponent(this[key]));
    }

    return out.join('&');
}

Or if you want a standalone function

或者如果你想要一个独立的功能

function arrayToQueryString(array_in){
    var out = new Array();

    for(var key in array_in){
        out.push(key + '=' + encodeURIComponent(array_in[key]));
    }

    return out.join('&');
}