0

I am a javascript newbie, Doubt well explained by simple example ?

    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 ?

2
  • What is i in your code, == "country"? Commented May 12, 2014 at 20:47
  • How about using a constructor? Commented May 12, 2014 at 20:49

2 Answers 2

3

You might use

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

to create such objects by new hash(). Or you'd just do

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

and use it like 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.


Are you sure you don't want a structure such as

var countryHash = {
     "africa": ["kenya", "egypt"],
     "asia": ["india", "china"],
     "amerika": ["usa", "canada"]
};
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use new hash(), then you can define hash() as a constructor function and do it like this:

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. 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:

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.