2011-04-06 67 views

回答

2

我可能是在簡化的問題,而只是使用本機連接符+

var format = var AppointmentSearchDaysAfter 
+ ' ' 
+ AppointmentSearchDaysAfterValue 
+ ',' 
+ AppointmentSearchDaysBefore 
+ ' ' 
+ AppointmentSearchDaysBeforeValue 
+ ',' 
+ PrimeSuiteId 
+ ' ' 
+ PrimeSuiteIdValue 
alert(format); 
2

你只追加使用 '+' 操作符的所有值:

實際字符串:

var result = AppointmentSearchDaysAfter + " " + AppointmentSearchDaysAfterValue + "," +AppointmentSearchDaysBefore + " " + AppointmentSearchDaysBeforeValue + "," + PrimeSuiteId + " " + PrimeSuiteIdValue; 

Readible字符串:

var result = AppointmentSearchDaysAfter + " " + 
       AppointmentSearchDaysAfterValue + "," + 
       AppointmentSearchDaysBefore + " " + 
       AppointmentSearchDaysBeforeValue + "," + 
       PrimeSuiteId + " " + 
       PrimeSuiteIdValue; 
4

你是否可以控制這些變量的名稱?如果是的話,我會改變它們的結構,像這樣:

var names = { 
    AppointmentSearchDays: 'aaa', 
    AppointmentSearchDaysBefore: 'bbb', 
    PrimeSuiteId: 'ccc'  
}; 

var values = { 
    AppointmentSearchDays: 3333, 
    AppointmentSearchDaysBefore: 5, 
    PrimeSuiteId: 10 
}; 

這將允許你合併它們像這樣:

var arr = []; 
for (var key in names) { 
    if (names.hasOwnProperty(key)) { 
     arr.push(names[key] + ' ' + values[key]); 
    } 
} 

arr.join(','); 

如果你想獲得真正的大膽,你可以這樣做:

var values = { 
    AppointmentSearchDays: { key: 'aaa', value: 3333 }, 
    AppointmentSearchDaysBefore: { key: 'bbb', value: 5 } 
    PrimeSuiteId: { key: 'ccc', value: 10 } 
}; 


var arr = []; 
for (var i = 0, len = values.length; i < len; i++) { 
    arr.push(values[i].key + ' ' + values[i].value); 
} 

arr.join(','); 
+1

+1我只是在輸入相同的東西:-)你可以調用var result = arr.join(); //逗號是默認分隔符 – daniellmb 2011-04-06 15:54:41

+0

+1配對輸出的好方法。過度殺傷其他;) – 2011-04-06 16:22:21

相關問題