只需使用常规的Javascript对象,该对象将“读取”与关联数组相同的方式。您还必须记住先初始化它们。
var obj = {};obj['fred'] = {};if('fred' in obj ){ } // can check for the presence of 'fred'if(obj.fred) { } // also checks for presence of 'fred'if(obj['fred']) { } // also checks for presence of 'fred'// The following statements would all workobj['fred']['apples'] = 1;obj.fred.apples = 1;obj['fred'].apples = 1;// or build or initialize the structure outrightvar obj = { fred: { apples: 1, oranges: 2 }, alice: { lemons: 1 } };如果要查看值,则可能会有类似以下内容:
var people = ['fred', 'alice'];var fruit = ['apples', 'lemons'];var grid = {};for(var i = 0; i < people.length; i++){ var name = people[i]; if(name in grid == false){ grid[name] = {}; // must initialize the sub-object, otherwise will get 'undefined' errors } for(var j = 0; j < fruit.length; j++){ var fruitName = fruit[j]; grid[name][fruitName] = 0; }}


