Javascript 从数组构造URL

Javascript 从数组构造URL,javascript,jquery,Javascript,Jquery,我有一个数组: var arr[]; 我在此插入了许多坐标(x和y值) 所以当我打印这个数组时,我得到一个字符串,比如,12,32,34,87,90,89。这些是坐标。我如何打破这些,并附加到一个字符串,这将是在格式 http://host/app?x1=12&y1=32&x2=32&y2=87&x3=90&y3=89 上面的URL可以有许多参数。如何从我的数组中构造上述URL。您可以构建查询字符串,在数组中循环(我在这里使用),并使用函数分隔坐标 var qs; $.each(arr,fun

我有一个数组:

var arr[];
我在此插入了许多坐标(
x
y
值)

所以当我打印这个数组时,我得到一个字符串,比如
,12,32,34,87,90,89
。这些是坐标。我如何打破这些,并附加到一个字符串,这将是在格式

http://host/app?x1=12&y1=32&x2=32&y2=87&x3=90&y3=89


上面的URL可以有许多参数。如何从我的数组中构造上述URL。

您可以构建查询字符串,在数组中循环(我在这里使用),并使用函数分隔坐标

var qs;
$.each(arr,function(i,v){
  qs += "x" + i + "=" + v.split("#")[0] + "&y" + i + "=" + v.split("#")[1] + ((i+1)!=arr.length) ? "&" : "";      
});

var url = "http://host/app?" + qs;

与上面的答案类似,循环通过,在散列上拆分,将片段添加到查询字符串中,如果不在末尾,则添加符号and

for(var c = 0, query = "http://host/app?", coordinate; c < arr.length; c++) {
  coordinate = arr[c].split("#");
  query += "x" + (c + 1) + "=" + coordinate[0] + "&";
  query += "y" + (c + 1) + "=" + coordinate[1];
  if(c < arr.length - 1) query += "&";
}
for(var c=0,query=”http://host/app?,坐标;c
我能想到的最简单的方法是:

var arr = [12+'#'+32, 34+'#'+87, 90+'#'+89],
    url = 'http://host/app',
    params = [], temp;

arr.forEach(function (a) {
    temp = a.split('#');
    params.push('x=' + temp[0] + 'y=' + temp[1]);
});

url += params.join('&');

console.log(url);
var arr=[12+'#'+32,34+'#'+87,90+'#'+89],
url='1〕http://host/app',
参数=[],温度;
arr.forEach(功能(a){
temp=a.split(“#”);
参数push('x='+temp[0]+'y='+temp[1]);
});
url+=params.join('&');

console.log(url)数组是如何构造的?只需在其中循环并拆分值。执行此操作时,将所有这些项附加到一个数组中,并在完成后加入。
var arr = [12+'#'+32, 34+'#'+87, 90+'#'+89],
    url = 'http://host/app',
    params = [], temp;

arr.forEach(function (a) {
    temp = a.split('#');
    params.push('x=' + temp[0] + 'y=' + temp[1]);
});

url += params.join('&');

console.log(url);