I want to populate a series of strings (beijingString, belingString etc) with values from an array ('contentStrings'); so as not to have to do:
beijingString = 'five strings';
berlinString = 'similar but different five strings';
bronxString = 'also similar but different five strings';
buenosairesString = 'similar again but subtly different five strings';
In the end I have 40 such strings to populate.
I tried putting the cities' string variable names into a second array ('cities') and looping through, assigning indexed values.
But it does not work.
Do I have to 'reference' (?) each variable as an element of the 'cities' array in some way, please?
TIA!
Full code snippet:
<!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>
-
Could you turn this into an object?Brett Weber– Brett Weber2014年09月05日 00:49:50 +00:00Commented Sep 5, 2014 at 0:49
3 Answers 3
A far better example is here: http://jsfiddle.net/L1dpt0bs/1/
You don't need to use any static array for cities.
var contentArrayLoop = 0;
for (contentArrayLoop = 0; contentArrayLoop < 4; contentArrayLoop++) {
var city = contentStrings[contentArrayLoop][1]
city = city.substring(0, city.indexOf(' '));
window[city + 'string'] = contentStrings[contentArrayLoop].join('');
};
alert ('after: ' + Beijingstring);
alert ('after: ' + Berlinstring);
alert ('after: ' + Bronxstring);
alert ('after: ' + Bueonosairesstring);
Comments
This isn't all of your data, but this is a format you could use to access the information easily:
var oContentObj =
{
buenos_airesContent :
{
url : 'http://www.buenosaires.com',
title : 'Buenos Aires title',
img : '<img src="./images/Buenos Aires.jpg">',
caption : 'Buenos Aires caption',
dsc : 'Buenos Aires description'
}
}
To loop through the properties of that object, use a for.. in loop :
for (var oCity in oContentObj)
{
// now you have a loop of the cities.. do stuff
for (var oProp in oCity)
{
// Now you have the properties of the city.. do more stuff
}
}
Or you could call the properties directly
oContentObj["CityName"]["CityProp"];
// or
oContentObj.CityName.CityProp;
And for a bigger picture on handling that data on the client,
that object can become much more than a container.
It can control handling that data as well. Containing, displaying, modifying and transporting if needed. :D
3 Comments
You can use concept of dynamic variable
var cities = ['beijing', 'berlin', 'bronx', 'bueonosaires'];
var contentArrayLoop = 0;
for (contentArrayLoop = 0; contentArrayLoop < 4; contentArrayLoop++) {
window[cities[contentArrayLoop] + 'string'] =
contentStrings[contentArrayLoop][0] +
contentStrings[contentArrayLoop][1] +
contentStrings[contentArrayLoop][2] +
contentStrings[contentArrayLoop][3] +
contentStrings[contentArrayLoop][4]
;
};
alert ('after: ' + beijingstring);
alert ('after: ' + berlinstring);
alert ('after: ' + bronxstring);
alert ('after: ' + bueonosairesstring);
Full code is here: