0

I am operating inside of a javascript for() loop and need to produce the following dynamically:

devCssFiles[ '../'+thisTheme._name+'/assets/css/main.css' ] = 
'child_themes/'+thisTheme._name+'/master.less';

the intended result is an associative array that can be declared iteratively inside of a parent object, e.g. (thisTheme._name is declared in an external JSON file which is read when the code below is processed, this part is working fine)

var myConfigObj = {};

for ( var key in thisThemeMetaObj ) {
   var devCssFiles = [],
      devCssFiles[ '../'+thisTheme._name+'/assets/css/main.css' ] = 
   'child_themes/'+thisTheme._name+'/master.less'; 
   myConfigObj[thisTheme._name] = devCssFiles;
}

I'm trying to give a complete explanation, but the problem is quite simple, I'm just declaring the named key of the associative array incorrectly at the line which reads devCssFiles[ '../'+thisTheme._name+'/assets/css/main.css' ] = 'child_themes/'+thisTheme._name+'/master.less';

Can someone show me the correct syntax here?

The intended output should be a JSON object like this:

    myConfigObject: {
        '../themeFoo/assets/css/main.css': [
            'child_themes/themeFoo/master.less'
        ]
    }
4

2 回答 2

1

语法错误在这里:

var devCssFiles = [],
      devCssFiles[ '../'+thisTheme._name+'/assets/css/main.css' ] = 
   'child_themes/'+thisTheme._name+'/master.less'; 

问题是devCssFiles = []需要一个分号(而不是逗号),以便在声明名为属性的变量时该变量存在。所以应该是

var devCssFiles = [];
   devCssFiles[ '../'+thisTheme._name+'/assets/css/main.css' ] = 
   'child_themes/'+thisTheme._name+'/master.less'; 
于 2013-02-24T01:19:41.150 回答
0

我想你想要

var myConfigObj = {};
for ( var key in thisThemeMetaObj ) {
   var thisTheme = thisThemeMetaObj[key],
       devCssFiles = [ 'child_themes/'+thisTheme._name+'/master.less' ]; // array literal
   myConfigObj['../'+thisTheme._name+'/assets/css/main.css'] = devCssFiles;
}

请注意,您devCssFiles是一个数组,但您确实在其上创建了一个非数字属性。

于 2013-02-24T01:22:35.480 回答