我有以下代码:
var imageLocations = [["grass", "grs.gif"], ["wood", "wd.gif"], ["sand", "snd.gif"]];
for (var i = 0; i < imageLocations.length; i++) {
tiles[imageLocations[i][0]] = new Image();
tiles[imageLocations[i][0]].src = imageLocations[i][1];
console.log(tiles);
};
此代码中我想要的输出是:
tiles.grass.src = "grs.gif";
tiles.wood.src = "wd.gif";
etc.
我得到的实际输出:
tiles.grass = [0, "grs.gif"];
tiles.wood = [1, "wd.gif"];
etc.
我的问题是如何防止数组索引也被添加到对象属性中?为什么src属性被忽略?
答案 0 :(得分:0)
不知道那边发生了什么,但以下代码对我有用:
var imageLocations = [["grass", "grs.gif"], ["wood", "wd.gif"], ["sand", "snd.gif"]];
var tiles = {};
for (var i = 0; i < imageLocations.length; i++) {
tiles[imageLocations[i][0]] = new Image();
tiles[imageLocations[i][0]]['src'] = imageLocations[i][1];
};
console.log(tiles);
工作示例: