How to merge values into an String format [duplicate]
Possible Duplicate:
Merging Values into an Array
Do you have control over the names of these variables? If so, I would change their structure like so:
var names = {
AppointmentSearchDays: 'aaa',
AppointmentSearchDaysBefore: 'bbb',
PrimeSuiteId: 'ccc'
};
var values = {
AppointmentSearchDays: 3333,
AppointmentSearchDaysBefore: 5,
PrimeSuiteId: 10
};
This would allow you to merge them like so:
var arr = [];
for (var key in names) {
if (names.hasOwnProperty(key)) {
arr.push(names[key] + ' ' + values[key]);
}
}
arr.join(',');
If you wanted to get real bold, you could do this:
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(',');
I may be over simplifying the question, but just use the native concatenation operator +
.
var format = var AppointmentSearchDaysAfter
+ ' '
+ AppointmentSearchDaysAfterValue
+ ','
+ AppointmentSearchDaysBefore
+ ' '
+ AppointmentSearchDaysBeforeValue
+ ','
+ PrimeSuiteId
+ ' '
+ PrimeSuiteIdValue
alert(format);
You would just append all of the values using the '+' operator:
Actual String :
var result = AppointmentSearchDaysAfter + " " + AppointmentSearchDaysAfterValue + "," +AppointmentSearchDaysBefore + " " + AppointmentSearchDaysBeforeValue + "," + PrimeSuiteId + " " + PrimeSuiteIdValue;
Readible String :
var result = AppointmentSearchDaysAfter + " " +
AppointmentSearchDaysAfterValue + "," +
AppointmentSearchDaysBefore + " " +
AppointmentSearchDaysBeforeValue + "," +
PrimeSuiteId + " " +
PrimeSuiteIdValue;
精彩评论