开发者

How to merge values into an String format [duplicate]

开发者 https://www.devze.com 2023-02-22 18:04 出处:网络
This question already has an answer here: Closed 11 years ago. 开发者_JAVA技巧 Possible Duplicate: Merging Values into an Array
This question already has an answer here: Closed 11 years ago. 开发者_JAVA技巧

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消