0

您好,我有 2 个数组,我想将它们放在一个字符串中。我的数组可以取从 0 到 maxNum-1 的值。所以假设我有 myArray1[i] 和 myArray2[i] 我想创建一个这样的字符串:

string = "myArray1ID" + 1 + "=" + (myArray1[1])
    + "&" +"myArray2ID" + 1 + "=" + (myArray2[1])
    + "&" + "myArray1ID" + 2 + "=" + (myArray1[2])
    + "&" + "myArray2ID" + 2 + "=" + (myArray2[2])
    + ......
    + "myArray1ID" + (maxNum - 1) + "=" + (myArray[maxNum-1])
    + "&" "myArray2ID" + (maxNum - 1) + "=" + (myArray2[maxNum-1]);

是否可以?

4

3 回答 3

3

使用循环的力量。

var output = [];

for (var i = 1; i < maxNum; ++i) {
    output.push(
        'myArray1ID' + i + '=' + myArray1[i],
        'myArray2ID' + i + '=' + myArray2[i]
    );
}
return output.join('&');
于 2012-06-08T07:54:34.857 回答
1
var myString = '';

for(var i; i < myArray1.length; i++){
  myString += "myArray1ID" + i + "=" + (myArray1[i]) + "&";
  myString += "myArray2ID" + i + "=" + (myArray2[i]) + "&";
}

//remove trialing "&"
var myString = myString.substring(0, myString.length-1);

这假设两个数组的长度相等

于 2012-06-08T07:55:38.257 回答
0

使用循环:

var stringArray = sep = ""; 
for(var i = 1; i < maxNum; i++) {
    stringArray += sep + "myArray1ID" + i + "=" + myArray1[i];
    stringArray += "&myArray2ID" + i + "=" + myArray2[i];
    sep = "&";
}

varstringArray应该包含您的数组值。

于 2012-06-08T07:54:49.987 回答