Jump to content

want some help with multidimensional array


salman233

Recommended Posts

hello guys

                          i want a little help with multidimensional arrays actually em making a currency convert with java script em stuck at point where i want to print a multidimensional array my code actually em a newbie

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
var Country = new Array('Pakistan','USA');   
// pakistan
Country['Pakistan'] = new Array('Punjab','Sindh');
Country['Pakistan']['Punjab'] = new Array('Faisalabad','Lahore','Multan');
Country['Pakistan']['Singh'] = new Array('Karachi');
// USA
Country['USA'] = new Array('New York');
Country['USA']['New York'] = new Array('New York','New York2');
/////////////////////////


    function changecountry(value){
    try{
        var state_area = document.getElementById('state_area');
        var state_drop = document.getElementById('state_drop');
        
        if(value==''){
            state_drop.innerHTML = '';
            state_area.style.display = 'none';
            //city_drop.innerHTML = '';
            //city_area.style.display = 'none';                   //empty check 
            return false;
        } 
        
        if(typeof Country[value] == 'undefined') return false;
        
        var newState = document.createElement('select');
        newState.name = 'state';
        newState.onChange = function (){
            alert("hello")
        }
        var State = Country[value]+[value];                                                   here is where i want to call country[state][city] from multi dimensional array  and print throught for loop 
        //alert(typeof nsewState.appendChild)
        for(var i=0,j=0; i<State.length,j<State[j].length; i++,j++){
            var newoptState = document.createElement('option');
            newoptState.value = State[i];
            newoptState.innerHTML = State[i];
            newState.appendChild(newoptState);
        }
        state_drop.innerHTML = '';
        state_drop.appendChild(newState);
        state_area.style.display = 'block';
    }catch(e){
        alert(e.message)
    }
    }
        
        
        
        function changecity(value){
    try{
        var city_area = document.getElementById('city_area');
        var city_drop = document.getElementById('city_drop');
        
        
        if(value==''){
        
        city_drop.innerHTML = '';
        city_area.style.display = 'none';                   //empty check 
                return false;
        } 
        
        
        
        
            if(typeof Country[value] == 'undefined') return false;
var newCity = document.createElement('select');
        newCity.name = 'city';
        newCity.onChange = function (){
            alert(23)
        }


        
        
        var City = Country[value];
        //alert(typeof newState.appendChild)
        for(var i=0; i<City.length; i++){
        
            var newoptCity = document.createElement('option');
            newoptCity.value = City[i];
            newoptCity.innerHTML = City[i];
            newCity.appendChild(newoptCity);
        }
        city_drop.innerHTML = '';
        city_drop.appendChild(newCity);
        city_area.style.display = 'block';
        
        
    }catch(e){
        alert(e.message)
    }
    }
</script>
</head>

<body>
    <form>
        Country: <select name="country" onChange="changecountry(this.value)">
            <option value="">Select</option>
            <option value="Pakistan">Pakistan</option>
            <option value="USA">USA</option>
        </select>
        <br>
        <div id="state_area" style="display:none;" onChange="changecity(this.value)">State: <span id="state_drop" onChange="changecity(this.value)"></span></div>
        <div id="city_area" style="display:none;">City: <span id="city_drop"></span></div>
    </form>
</body>
</html>

 

 

i want to call country[state][city] through for loop  but gives a error

but when i call country[state]  only it,s displays country and onclick displays states

please help me out where em wrong

 

Link to comment
Share on other sites

ok thanks guys question in short is that  this is a var State= country['value']  if i print it with for loop it displays result now further printing it like this var State2= country['value1']['value2'] with for loop it don,t shows any result please tell me where i am wrong ?

Link to comment
Share on other sites

1) Tell us what you have, what your current code is doing

2) Tell us what you want your code to do

3) Show us the code that you currently have that is causing you the problem

4) Give an example of what the desired output is

 

If you do these four things, it will be much easier to help you.

Link to comment
Share on other sites

var Country = new Array('Pakistan','USA'); 

// pakistan

Country['Pakistan'] = new Array('Punjab','Sindh');

Country['Pakistan']['Punjab'] = new Array('Faisalabad','Lahore','Multan');

Country['Pakistan']['Singh'] = new Array('Karachi');

// USA

Country['USA'] = new Array('New York');

Country['USA']['New York'] = new Array('New York','New York2');

 

  var State = Country[value][value];

 

for(i=0; i<State.length; i++)

{

 

document.write(State);

 

}

 

 

this code displays nothing i want to display multidimensional array using for loop but it displays nothing

Link to comment
Share on other sites

ok haku bro it,s like this  i want to print countries using for loop  i have new array country= new array('pakistan','usa');

ok now i want to print states of both countries['pakistan'] = new array ('punjab','sindh); country['usa']= new array ('bla','bla');

i want to do this using for loop my current code is shown above it displays nothing i want to display stated and countries meaning i want to print whole array

 

Link to comment
Share on other sites

So am I:

1) Tell us what you have, what your current code is doing

2) Tell us what you want your code to do

3) Show us the code that you currently have that is causing you the problem

4) Give an example of what the desired output is

 

If you do these four things, it will be much easier to help you.

Link to comment
Share on other sites

  • 2 weeks later...

This thread is brilliant.  Also, I'm sorry mate, I've tried going through your code but it is pretty unfathomable.  Although I think you may be wanting to use an object instead of an array:

var country = {
  'Pakistan' : {
    'Punjab' : ['Faisalabad','Lahore','Multan'],
    'Sindh' : ['Karachi']
  },
  'USA' : {
    'New York': ['New York','New York2']
  }
};

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.