简体   繁体   English

如何在JavaScript中创建hashmap实例?

[英]How to create instance of hashmap in javascript?

I am a javascript newbie, Doubt well explained by simple example ? 我是javascript新手,通过简单的示例可以很好地解释吗?

    var hashAfrica = { "country" : new Array()};

    // populate all countries in africa.
    var arr = hashAfrica["country"];
    arr.push("kenya");
    arr.push("egypt");

    var hashAsia = { "country" : new Array()};

    // populate all capitals in asia.
    var arr = hashAsia["country"];
    arr.push("india");
    arr.push("china");

    var hashAmerica = { "country" : new Array()};

    // populate all capitals in america.
    var arr = hashAmerica["country"];
    arr.push("usa");
    arr.push("canada");

What I want to do is: 我想做的是:

var hash = { "country" : new Array()};
var hashAfica = new hash();
var hashAsia = new hash();
var hashAmerica = new hash();

How to accomplish this in javascript ? 如何在javascript中完成此操作?

You might use 你可能会用

function hash() {
    this.country = []; // or: new Array();
}

to create such objects by new hash() . 通过new hash()创建此类对象。 Or you'd just do 否则你会做

function getCountryArray() {
     return {country: []};
}

and use it like hashAfrica = getCountryArray() . 并像hashAfrica = getCountryArray()一样使用它。


But please notice that you are not using anything similiar to a hash map here. 但是请注意,您在这里没有使用任何类似于哈希图的东西。 You're not using all those arrays as hashmaps, but as simple lists. 您并不是将所有这些数组都用作哈希图,而是用作简单列表。 If you want to make some kind of map in JavaScript, simply use an object to which you dynamically add keys. 如果要使用JavaScript进行某种映射,只需使用一个向其动态添加键的对象。


Are you sure you don't want a structure such as 您确定不想要这样的结构吗

var countryHash = {
     "africa": ["kenya", "egypt"],
     "asia": ["india", "china"],
     "amerika": ["usa", "canada"]
};

If you want to use new hash() , then you can define hash() as a constructor function and do it like this: 如果要使用new hash() ,则可以将hash()定义为构造函数,并按如下所示进行操作:

function hash() {
    this.country = [];
}

var hashAfrica = new hash();
hashAfrica.country.push("kenya");
hashAfrica.country.push("egypt");

// and so on...
var hashAsia = new hash();
var hashAmerica = new hash();

Personally, I wouldn't call this a hash , since it doesn't really have anything to do with a hash object. 就我个人而言,我不会将其称为hash ,因为它实际上与哈希对象没有任何关系。 You're just storing an array of countries in an object. 您只是在一个对象中存储一组国家/地区。 I'd probably call it something like continentInfo and also define a constructor that could populate the initial country list: 我可能会称其为continentInfo之类的东西,并定义一个可以填充初始国家/地区列表的构造函数:

function ContinentInfo(list) {
    // if initial argument passed, then split it into an array
    // and set initial country array from it
    if (list) {
        this.countries = list.split(" ");
    } else {
        this.countries = [];
    }
}

var africaInfo = new ContinentInfo("kenya egypt");
var asiaInfo = new ContinentInfo("india china");
var americaInfo = new ContinentInfo("usa canada");

Of, if you actually want a hash of the continents, you can do this: 其中,如果您实际上想要散列各大洲,则可以执行以下操作:

var continents = {};
continents["africa"] = new ContinentInfo("kenya egypt");
continents["asia"] = new ContinentInfo("india china");
continents["america"] = new ContinentInfo("usa canada");

This would give you a lookup by continent name and then each continent structure would have a list of countries and then any other data: 这将使您按洲名称进行查找,然后每个洲结构将具有国家/地区列表,然后包含任何其他数据:

var asiaCountries = continents["asia"].countries;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM