JavaScript:将一个数组中的值分配给另一个数组中的变量

时间:2014-09-05 00:31:49

标签: javascript arrays

我想用数组中的值填充一系列字符串(beijingString,belingString等)('contentStrings');以免这样做:

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个这样的字符串来填充。

我尝试将城市的字符串变量名称放入第二个数组('cities')并循环,分配索引值。

但它不起作用。

我是否必须以某种方式“引用”(?)每个变量作为'cities'数组的元素?

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>

3 个答案:

答案 0 :(得分:1)

这里有一个更好的例子: http://jsfiddle.net/L1dpt0bs/1/

您不需要为城市使用任何静态数组。

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

答案 1 :(得分:0)

您可以使用动态变量的概念

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

完整代码在这里:

http://jsfiddle.net/m745odgf/

答案 2 :(得分:0)

这不是您的所有数据,但这是一种可以轻松访问信息的格式:

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'
   }
}

要遍历该对象的属性,请使用for .. in循环:

   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
      }
   }

或者您可以直接调用属性

oContentObj["CityName"]["CityProp"];
// or
oContentObj.CityName.CityProp;

有关在客户端处理该数据的更大图景,

that object can become much more than a container

它也可以控制对数据的处理。如果需要,包含,显示,修改和运输。 :D