我想用數組中的值('contentStrings')填充一系列字符串(beijingString,belingString等);所以沒有必要這樣做:JavaScript:將數組中的值賦值給另一個變量
beijingString = 'five strings';
berlinString = 'similar but different five strings';
bronxString = 'also similar but different five strings';
buenosairesString = 'similar again but subtly different five strings';
最後我有40個這樣的字符串填充。
我試着把城市的字符串變量名放入第二個數組(城市),並循環遍歷,分配索引值。
但它不起作用。
我需要以某種方式將每個變量作爲「城市」數組的元素「引用」(?)嗎?
TIA!
完整的代碼片段:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var beijingContentString = '';
var berlinContentString = '';
var bronxContentString = '';
var buenos_airesContentString = '';
var contentStrings = [
['http://www.beijing.com',
'Beijing title',
'<img src="./images/beijing.jpg">',
'Beijing caption',
'Beijing description'
],
['http://www.berlin.com',
'Berlin title',
'<img src="./images/berlin.jpg">',
'Berlin caption',
'Berlin description'
],
['http://www.bronx.com',
'Bronx title',
'<img src="./images/Bronx.jpg">',
'Bronx caption',
'Bronx description'
],
['http://www.buenosaires.com',
'Buenos Aires title',
'<img src="./images/Buenos Aires.jpg">',
'Buenos Aires caption',
'Buenos Aires description'
]
];
var beijingString = '';
var berlinString = '';
var bronxString = '';
var bueonosairesString = '';
alert ('before: ' + beijingString);
alert ('before: ' + berlinString);
alert ('before: ' + bronxString);
alert ('before: ' + bueonosairesString);
var cities = [beijingString, berlinString, bronxString, bueonosairesString];
var contentArrayLoop = 0;
for (contentArrayLoop = 0; contentArrayLoop < 4; contentArrayLoop++) {
cities[contentArrayLoop]=
contentStrings[contentArrayLoop][0] +
contentStrings[contentArrayLoop][1] +
contentStrings[contentArrayLoop][2] +
contentStrings[contentArrayLoop][3] +
contentStrings[contentArrayLoop][4]
;
alert(cities[contentArrayLoop]);
};
alert ('after: ' + beijingString);
alert ('after: ' + berlinString);
alert ('after: ' + bronxString);
alert ('after: ' + bueonosairesString);
</script>
</body>
</html>
你可以把它變成一個對象嗎? – 2014-09-05 00:49:50