从循环返回逗号分隔的字符串

时间:2014-04-14 17:12:12

标签: javascript jquery

您好我正在尝试从循环中返回逗号分隔的字符串。

在我console.log的那一刻,它在每一行都给我这个。

  

used vehicles; 3374304; 1 ;;; evar1 = 44377AA | evar49 = undefined:null ContentServer?pagename = eUsed / script / usedcars-compare:784   二手车辆; 3407554; 1 ;;; evar1 = 61671CA | evar49 = undefined:null ContentServer?pagename = eUsed / script / usedcars-compare:784   二手车辆; 3374316; 1 ;;; evar1 = 44377AA | evar49 = undefined:null

我需要这个:

  

二手车辆; 3374304; 1 ;;; evar1 = 44377AA | evar49 = undefined:null,二手车辆; 3407554; 1 ;;; evar1 = 61671CA | evar49 = undefined:null,二手车辆; 3374316; 1 ;;; evar1 = 44377AA | evar49 =未定义:空

这是我的代码:

function compareTrackingValues(){

    var getPostcode = JSON.parse(sessionStorage.getItem('currentPageData')),
        postcode = getPostcode.vehicleSessionData.location_entry,
        convertCompareObj,
        vehicleObj = [],
        vehicleInfo = {};

        for(var key in vehicleData){

            vehicleInfo = {};

            vehicleInfo.vehicleID = vehicleData[key].vehicleId;
            vehicleInfo.dealerID = vehicleData[key].vendor.vendorCode;
            vehicleInfo.scheme = vehicleData[key].vehicleId.schemes;
            vehicleInfo.owner = vehicleData[key].vendor.ownerCode;

            vehicleObj.push(vehicleInfo);



            s.Products = "used vehicles;"+vehicleInfo.vehicleID;
            s.Products += ";1;;;evar1="+vehicleInfo.dealerID ;
            s.Products += "|evar49="+vehicleInfo.scheme;
            s.Products += ":"+vehicleInfo.owner;

            var test = s.Products;
            console.log(test);


        }

1 个答案:

答案 0 :(得分:2)

而不是

for(var key in vehicleData){
    var test = /* ... */;
    console.log(test);
}

使用

var test = [];
for(var key in vehicleData){
    test.push(/* ... */);
}
console.log(test.join(', '));