I am trying to understand how 2D Arrays should work in Javascript, where I am currently attempting to store data in categories. In this scenario, I have a list of maps such as...
我试图了解2D数组应如何在Javascript中工作,我目前正在尝试将数据存储在类别中。在这种情况下,我有一个地图列表,如...
arena_badlands
arena_dust
ctf_2fort
cp_dustbowl
I would like to create a 2D Array where they are categorized by the types (arena, ctf, cp) so I can handle the data more efficiently. For whatever reason, I am unable to use the push()
method as expected. Can anyone please help share what it is that I am doing incorrectly?
我想创建一个2D数组,它们按类型(竞技场,ctf,cp)进行分类,这样我就可以更有效地处理数据。无论出于何种原因,我无法按预期使用push()方法。谁能帮助分享我做错了什么?
As the maps in the list won't necessarily be in order by type, it keeps me from creating a single array of arena maps and then placing this inside of availableMaps.
由于列表中的地图不一定按类型排序,因此它不会创建单个竞技场地图数组,然后将其放在availableMaps中。
var availableMaps = [[]]; // Store all maps on the server which can be loaded
function getMaps()
{
$.ajax({
type: "POST",
url: "handler.php",
data: { 'action': 'getMaps' },
dataType: 'json',
success: function(data)
{
var maps = data['maps'];
var counter = 0;
$.each( maps, function( key, value )
{
// Split map name to fetch the type (ex: arena_badlands would be an 'arena' map)
var parts = value.split("_");
var mapType = parts[0];
availableMaps[mapType][counter].push(value);
counter++;
});
}
});
}
1 个解决方案
#1
Here is one way:
这是一种方式:
var inputs = ["arena_badlands", "arena_dust", "ctf_2fort", "cp_dustbowl"];
function getAvailableMaps(inputs) {
var map = {}, parts, name, item, items;
for (var i = 0; i < inputs.length; ++i) {
parts = inputs[i].split('_');
name = parts[0];
item = parts.slice(1).join("");
items = map[name] || (map[name] = []);
items.push(item);
}
return map;
}
var availableMaps = getAvailableMaps(inputs);
It creates an object at the root for holding named indexes, and uses an array to store the list of items.
它在根目录下创建一个用于保存命名索引的对象,并使用数组来存储项目列表。
Here is another approach as well:
这是另一种方法:
var inputs = ["arena_badlands", "arena_dust", "ctf_2fort", "cp_dustbowl"];
function getAvailableMaps(inputs) {
var map = {}, input, name, item, items;
for (var i = 0, i2; i < inputs.length; ++i) {
input = inputs[i];
i2 = input.indexOf("_");
if (i2 == -1) continue; // (or throw an error)
name = input.substring(0, i2);
item = input.substring(i2+1);
items = map[name] || (map[name] = []);
items.push(item);
}
return map;
}
var availableMaps = getAvailableMaps(inputs);
#1
Here is one way:
这是一种方式:
var inputs = ["arena_badlands", "arena_dust", "ctf_2fort", "cp_dustbowl"];
function getAvailableMaps(inputs) {
var map = {}, parts, name, item, items;
for (var i = 0; i < inputs.length; ++i) {
parts = inputs[i].split('_');
name = parts[0];
item = parts.slice(1).join("");
items = map[name] || (map[name] = []);
items.push(item);
}
return map;
}
var availableMaps = getAvailableMaps(inputs);
It creates an object at the root for holding named indexes, and uses an array to store the list of items.
它在根目录下创建一个用于保存命名索引的对象,并使用数组来存储项目列表。
Here is another approach as well:
这是另一种方法:
var inputs = ["arena_badlands", "arena_dust", "ctf_2fort", "cp_dustbowl"];
function getAvailableMaps(inputs) {
var map = {}, input, name, item, items;
for (var i = 0, i2; i < inputs.length; ++i) {
input = inputs[i];
i2 = input.indexOf("_");
if (i2 == -1) continue; // (or throw an error)
name = input.substring(0, i2);
item = input.substring(i2+1);
items = map[name] || (map[name] = []);
items.push(item);
}
return map;
}
var availableMaps = getAvailableMaps(inputs);