我有这个功能:
function composeLootTables(lootType, result) {
for (var d in lootType.items) {
if (result[lootType.title] === undefined) {
result[lootType.title] = [[0],[0]];
}
result[lootType.title][d][0] = lootType.items[d][0];
result[lootType.title][d][1] = lootType.items[d][1];
composeLootTables(lootType.items[d][0], result);
}
return result;
}
首先它解析这个:
residential : {
title: "residential",
items:[
[generic, 0.7],
[military, 0.7],
[hospital, 0.7],
[Colt1911, 0.5]
]
},
然后其他 lootType 成为其中之一:
var Colt1911 = {
title: "Colt 1911"
};
var generic = {
title: "Generic",
items: [[tin_can, 0.2],[jelly_bean, 0.3]]
};
var military = {
title: "Military",
items: [[bfg, 0.2],[akm, 0.3]]
};
var hospital = {
title: "Hospital",
items: [[condoms, 0.2],[zelyonka, 0.3]]
};
所以,麻烦就在这个字符串中:
result[lootType.title][d][0] = lootType.items[d][0];
result[lootType.title][d][1] = lootType.items[d][1];
Uncaught TypeError: Cannot set property '0' of undefined
根据console.log,result[lootType][d] === undefined
仅当“d”变为2或3时(其他时候“d”=== 0或1)。
我假设如果我将值分配给数组的未定义字段,它将被这个值填充。
我已经找到解决方案 -
result[lootType.title][d] = lootType.items[d];
工作正常,它返回正确的二维数组,但我想知道这些数组是怎么回事。