多维数组的问题

时间:2012-11-28 02:00:40

标签: javascript multidimensional-array tracking

我遇到了这个darn数组的问题。这是发布我的信息看起来像这样。任何想法如何解决这个问题?

prdpr=10.95^TBCC9^2^Shoes
prdsku=2.50^TDxa2^1^Pants
prdqn=7.50^Tasds^1^Hats
prdcatid=undefined^undefined^undefined^undefined

它应该是什么样的:

prdpr=10.95^2.50^7.50
prdsku=TBCC9^TDxa2^Tasds
prdqn=2^1^1
prdcatid=Shoes^Pants^Hats

稍后我会串起来寻找一个网址

var advid = "xxx";
var oid = "xxx";
var amt = "20.95";

// This array I cannot mess with, this is just an example
var OrderDetails = new Array();
OrderDetails[0] = ['10.95','2.50','7.50'];
OrderDetails[1] = ['TBCC9','TDxa2','Tasds'];
OrderDetails[2] = ['2','1','1'];
OrderDetails[3] = ['Shoes','Pants','Hats'];

var prdpr = '';
var prdsku = '';
var prdqn = '';
var prdcatid = '';

for(var x = 0; x < OrderDetails.length; x++) {
    var delim = "";
    if(x == 0){
        delim = "";
    }  else{
        delim = "^";
    }

    prdsku += delim + OrderDetails[x][0];
    prdpr += delim + OrderDetails[x][1];
    prdqn += delim + OrderDetails[x][2];
    prdcatid += delim + OrderDetails[x][3];
}

var output = '<div>Product Sku=' + prdsku + 'Item Cost=' + prdpr + 'Quanty=' + prdqn + 'Category=' + prdcatid + '</div>';
document.write(output);

1 个答案:

答案 0 :(得分:1)

var OrderDetails = new Array();
OrderDetails[0] = ['10.95','2.50','7.50'];
OrderDetails[1] = ['TBCC9','TDxa2','Tasds'];
OrderDetails[2] = ['2','1','1'];
OrderDetails[3] = ['Shoes','Pants','Hats'];

var delim = '^';

var prdpr = OrderDetails[0].join(delim);
var prdsku = OrderDetails[1].join(delim);
var prdqn = OrderDetails[2].join(delim);
var prdcatid = OrderDetails[3].join(delim);