Javascript 如何基于填充数组动态填充值数组?

Javascript 如何基于填充数组动态填充值数组?,javascript,arrays,Javascript,Arrays,我已经做了一段时间了,所以我决定走出nodejs进入jsfiddle,看看你们能不能给我一些启示 代码如下: inventory = [ // 50 Slot inventory 10 HORIZONTAL / 5 SLOTS VERTICAL 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

我已经做了一段时间了,所以我决定走出
nodejs
进入jsfiddle,看看你们能不能给我一些启示

代码如下:

 inventory = [ // 50 Slot inventory 10 HORIZONTAL / 5 SLOTS VERTICAL
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 ]
 items = {
    "1": {
        name: "Short Sword",
        slot_position: 5,
        slot_size: [1, 3]
    },
    "2": {
        name: "Heavy Leather Boots",
        slot_position: 1,
        slot_size: [2, 2]
    },
    "3": {
        name: "Potion 1",
        slot_position: 26,
        slot_size: [1, 1]
    }
 }

 for (i in items) {
    inventory[items[i].slot_position] = 1; // Fill in the used inventory slots to 1 (The easy part)

    if (items[i].slot_size) {
        if (items[i].slot_size[0] > 1) {
            /*
            The X slot_size has to be greater than '1' because we already filled in their current positon.
            Now I need to fill in the inventory slots based on their item_slot_size [X,Y] values... (I'm              stuck here)
            */



        }
    }


 }

 console.log(inventory);
 console.log(inventory.length); // 50 is correct.
和JSFIDLE:

在第
42行
,我被卡在这里,因为我需要根据物料槽尺寸动态地将库存中的槽填充到
1

例如,短剑的插槽大小为
[1,3]
(向下3个方块),那么如何在
库存中动态填充相应的值


我的
slot\u size
数组如何使用的一个示例最好留待我的图表来查看:

这是我想到的第一件事:

// slot_to_coords(n) returns (n%10, n/10)
// coords_to_lot(x, y) returns (y*10 + x)
coords = slot_to_coords(items[i].slot_position);
for (int x = 0; x < items[i].slot_size[0]; x++) {
    for (int y = 0; y < items[i].slot_size[1]; y++) {
        slot = coords_to_slot(x+coords[0], y+coords[1]);
        inventory[slot] = 1;
    }
}
//槽对槽(n)返回(n%10,n/10)
//协调批量(x,y)退货(y*10+x)
坐标=槽到槽坐标(项目[i]。槽位置);
对于(int x=0;x

这和你想做的相似吗?确保获得所有边缘案例。

这是我想到的第一件事:

// slot_to_coords(n) returns (n%10, n/10)
// coords_to_lot(x, y) returns (y*10 + x)
coords = slot_to_coords(items[i].slot_position);
for (int x = 0; x < items[i].slot_size[0]; x++) {
    for (int y = 0; y < items[i].slot_size[1]; y++) {
        slot = coords_to_slot(x+coords[0], y+coords[1]);
        inventory[slot] = 1;
    }
}
//槽对槽(n)返回(n%10,n/10)
//协调批量(x,y)退货(y*10+x)
坐标=槽到槽坐标(项目[i]。槽位置);
对于(int x=0;x

这和你想做的相似吗?确保获得所有边缘案例。

这是我想到的第一件事:

// slot_to_coords(n) returns (n%10, n/10)
// coords_to_lot(x, y) returns (y*10 + x)
coords = slot_to_coords(items[i].slot_position);
for (int x = 0; x < items[i].slot_size[0]; x++) {
    for (int y = 0; y < items[i].slot_size[1]; y++) {
        slot = coords_to_slot(x+coords[0], y+coords[1]);
        inventory[slot] = 1;
    }
}
//槽对槽(n)返回(n%10,n/10)
//协调批量(x,y)退货(y*10+x)
坐标=槽到槽坐标(项目[i]。槽位置);
对于(int x=0;x

这和你想做的相似吗?确保获得所有边缘案例。

这是我想到的第一件事:

// slot_to_coords(n) returns (n%10, n/10)
// coords_to_lot(x, y) returns (y*10 + x)
coords = slot_to_coords(items[i].slot_position);
for (int x = 0; x < items[i].slot_size[0]; x++) {
    for (int y = 0; y < items[i].slot_size[1]; y++) {
        slot = coords_to_slot(x+coords[0], y+coords[1]);
        inventory[slot] = 1;
    }
}
//槽对槽(n)返回(n%10,n/10)
//协调批量(x,y)退货(y*10+x)
坐标=槽到槽坐标(项目[i]。槽位置);
对于(int x=0;x

这和你想做的相似吗?确保获得所有边缘案例。

首先,您的库存应该是一个矩阵(集合集合)

然后可以使用插槽大小进行迭代。此外,插槽位置应存储在坐标中:

 items = {
    "1": {
        name: "Short Sword",
        slot_position: [5,0],
        slot_size: [1, 3]
    },
    "2": {
        name: "Heavy Leather Boots",
        slot_position: [1,0],
        slot_size: [2, 2]
    },
    "3": {
        name: "Potion 1",
        slot_position: [6,2],
        slot_size: [1, 1]
    }
 }

 for (i in items) {
    var item = items[i];
    if (item.slot_size) {
            for (var x = 0; x < item.slot_size[0]; x++) {
                for (y = 0; y < item.slot_size[1]; y++) { 
                     inventory[y+item.slot_position[1]][x+item.slot_position[0]] = item;               
            }
        }
    }
 }
项目={
"1": {
名称:“短剑”,
插槽位置:[5,0],
插槽大小:[1,3]
},
"2": {
名称:“重型皮靴”,
插槽位置:[1,0],
插槽大小:[2,2]
},
"3": {
名称:“药剂1”,
插槽位置:[6,2],
插槽大小:[1,1]
}
}
(i)在项目中{
var项目=项目[i];
if(项目槽尺寸){
对于(变量x=0;x

首先,您的库存应该是一个矩阵(集合集合)

然后可以使用插槽大小进行迭代。此外,插槽位置应存储在坐标中:

 items = {
    "1": {
        name: "Short Sword",
        slot_position: [5,0],
        slot_size: [1, 3]
    },
    "2": {
        name: "Heavy Leather Boots",
        slot_position: [1,0],
        slot_size: [2, 2]
    },
    "3": {
        name: "Potion 1",
        slot_position: [6,2],
        slot_size: [1, 1]
    }
 }

 for (i in items) {
    var item = items[i];
    if (item.slot_size) {
            for (var x = 0; x < item.slot_size[0]; x++) {
                for (y = 0; y < item.slot_size[1]; y++) { 
                     inventory[y+item.slot_position[1]][x+item.slot_position[0]] = item;               
            }
        }
    }
 }
项目={
"1": {
名称:“短剑”,
插槽位置:[5,0],
插槽大小:[1,3]
},
"2": {
名称:“重型皮靴”,
插槽位置:[1,0],
插槽大小:[2,2]
},
"3": {
名称:“药剂1”,
插槽位置:[6,2],
插槽大小:[1,1]
}
}
(i)在项目中{
var项目=项目[i];
if(项目槽尺寸){
对于(变量x=0;x

首先,您的库存应该是一个矩阵(集合集合)

然后可以使用插槽大小进行迭代。此外,插槽位置应存储在坐标中:

 items = {
    "1": {
        name: "Short Sword",
        slot_position: [5,0],
        slot_size: [1, 3]
    },
    "2": {
        name: "Heavy Leather Boots",
        slot_position: [1,0],
        slot_size: [2, 2]
    },
    "3": {
        name: "Potion 1",
        slot_position: [6,2],
        slot_size: [1, 1]
    }
 }

 for (i in items) {
    var item = items[i];
    if (item.slot_size) {
            for (var x = 0; x < item.slot_size[0]; x++) {
                for (y = 0; y < item.slot_size[1]; y++) { 
                     inventory[y+item.slot_position[1]][x+item.slot_position[0]] = item;               
            }
        }
    }
 }
项目={
"1": {
名称:“短剑”,
插槽位置:[5,0],
插槽大小:[1,3]
},
"2": {
名称:“重型皮靴”,
插槽位置:[1,0],
插槽大小:[2,2]
},
"3": {
名称:“药剂1”,
插槽位置:[6,2],
插槽大小:[1,1]
}
}
(i)在项目中{
var项目=项目[i];
if(项目槽尺寸){
对于(变量x=0;x

首先,您的库存应该是一个矩阵(集合集合)

然后可以使用插槽大小进行迭代。此外,插槽位置应存储在坐标中:

 items = {
    "1": {
        name: "Short Sword",
        slot_position: [5,0],
        slot_size: [1, 3]
    },
    "2": {
        name: "Heavy Leather Boots",
        slot_position: [1,0],
        slot_size: [2, 2]
    },
    "3": {
        name: "Potion 1",
        slot_position: [6,2],
        slot_size: [1, 1]
    }
 }

 for (i in items) {
    var item = items[i];
    if (item.slot_size) {
            for (var x = 0; x < item.slot_size[0]; x++) {
                for (y = 0; y < item.slot_size[1]; y++) { 
                     inventory[y+item.slot_position[1]][x+item.slot_position[0]] = item;               
            }
        }
    }
 }
项目={
"1": {
名称:“短剑”,
插槽位置:[5,0],
插槽大小:[1,3]
},
"2": {
名称:“重型皮靴”,
插槽位置:[1,0],
插槽大小:[2,2]
},
"3": {
名称:“药剂1”,
插槽位置:[6,2],
插槽大小:[1,1]
}
}
(i)在项目中{
var项目=项目[i];
if(项目槽尺寸){
对于(变量x=0;x