SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    searching for array title

    I have five arrays outside of a function. What I am trying to achieve is when itemDetail is
    concatenated it will give me the name of that array and I will be able to access the elements
    but how do I call an array when the var is a string if I itemDetail[2] it will goto the letter in the
    string? thanks


    Code:
    itemA = new Array(a,b,c);
    itemB = new Array(a,b,c);
    itemC = new Array(a,b,c);
    itemD = new Array(a,b,c);
    itemE = new Array(a,b,c);
    
    function arrayFind (itemVar){
    
    			var itemDetail = "item"+itemVar;
    			alert(itemDetail);
                             // get these elements
    }
    arrayFind ('C');

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Location
    no fixed address
    Posts
    851
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One option with a slightly different approach:

    Code:
            
         <script type="text/javascript">
                var items = [];
                items['A'] = [1,2,3];
                items['B'] = [4,5,6];
                items['C'] = [7,8,9];
    
                function arrayFind(itemVar){
                    var str='';
                    for(i=0; i<items[itemVar].length; i++){
                        str += items[itemVar][i]+"\n";
                    }
                    alert(str);
                }
    
                arrayFind('B');  //outputs 4,5,6
    
            </script>

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did not know how powerful the code was until I started playing i with it thanks again


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •